php find and remove from content img string - web-scraping

I have a little problem, how correct can i find img src string who ends with dealer.jpg and remove only this string from my content? for example:
<?php
$content = '<b>this is a content</b><img src=http://adress.com/as5.jpg><br> this is a content <img src=http://www.another-adress.com/dealer.jpg>';
$inf = explode("/dealer.jpg", $content);
$string = str_replace("<img src=\"$inf[0]/dealer.jpg\">", "", $content);
?>
I use this because I dont know image link, link always is another, but I know this link ends with dealer.jpg , and this script is not working... so can some one help me how correct do this ? normaly I whant to remove ads from page that i scraped. Thank you !

If i understood correctly you are trying to remove the img tag that ends with "dealer.jpg" (no matter the domain), right? try this:
$content = '<b>this is a content</b><img src=http://adress.com/as5.jpg><br> this is a content <img src=http://www.another-adress.com/dealer.jpg>';
$content = preg_replace('/<img src=[A-z0-9-_":\.\/]+\/dealer\.jpg>/', '', $content);
var_dump($content);
Edit
This second example will match the img tag even if it has another attributes such as alt, width, etc (but again, must end with "dealer.jpg")
$content = '<b>this is a content</b><img src="http://adress.com/as5.jpg"><br> this is a content <img alt="dealer-image" width="120" height="40" src="http://www.another-adress.com/dealer.jpg">';
$content = preg_replace('/<img[A-z0-9-_:="\.\/ ]+src="[A-z0-9-_:\.\/]+\/dealer\.jpg">/', '', $content);
var_dump($content);
Obs: I changed the initial $content because i've noticed it was missing the double quotation for src attribute. Not sure if was a typo or your string really looks like this.
Edit 2
Here is a example using DOM (a guess that is the best aproach here since the order of attributes could change):
$content = '<b>this is a content</b><img src="http://adress.com/as5.jpg"><br> this is a content <img alt="dealer-image" width="120" height="40" src="http://www.another-adress.com/dealer.jpg">';
// creates a DOMDocument based on your string, and wraps it in a div
$dom = new DOMDocument();
$dom->loadHTML("<div>{$content}</div>", LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED);
// get all img tags
$imgs = $dom->getElementsByTagName('img');
foreach ($imgs as $img) { // if they have that src, remove it from $dom
if (strpos($img->getAttribute('src'), 'dealer.jpg')) {
$img->parentNode->removeChild($img);
};
}
// get all the content of my first div, and print it
$newContent = $dom->getElementsByTagName('div')->item(0);
foreach ($newContent->childNodes as $childNode) {
var_dump($dom->saveHTML($childNode));
}

Related

preg_replace specific Text to small latter strtolower

Please i need to replace link with capital letter to small letter using wp function
E.g let every text after /get/ be replace with small letter.
<a href="https://stackexchange.com/get/ASK-Question"> => <a href="https://stackexchange.com/get/ask-question">
<a href="https://stackexchange.com/get/aSK-QuesTion"> => <a href="https://stackexchange.com/get/ask-question">
here is my function, I dont know where i am getting it wrong. however i am not good in wp. please correct me by posting the full correct code.
function emailleftappend($content){
$findleft = '/get\/(?<=\/)([A-Za-z]+?) ([A-Za-z]+?)(?=\/">)/m';
$replaceleft = '$1-$2';
$content = preg_replace(strtolower($findleft), $replaceleft, $content);
return $content;
}
add_filter('the_content', 'emailleftappend');
function emailleftappend($content){
$content = preg_replace_callback('/(?<=get\/)(.*?)-(.*?)(?=\/">)/', function ($m) {
return sanitize_title($m[1]). '-'. sanitize_title($m[2]); }, $content);
return $content;
}
add_filter('the_content', 'emailleftappend');
Finally fixed.

shortcode to find and replace the url and image src

i have to get the content and find the url from the content and replace it with the another default url.whatever content is there i have to find the href and replace it with the default url.
The shortcode changes every url to a new random cat using this simple API: http://random.cat/meow
Example:
[demo_shortcode]<img alt="A boring image of something other than a cat." src="http://i.imgur.com/BTNIDBR.gif">[/demo_shortcode]
Outputs:
<img alt="A boring image of something other than a cat." src="http://random.cat/i/zUhVw.jpg">
for try i am trying this to get
function demo_function( $atts, $content = ""){
$pattern = '(<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>)';
$varr=file_get_contents('http://random.cat/meow');
$content = preg_replace($pattern,$varr,$content);
return $content;
}
add_shortcode( 'demo_shortcode', 'demo_function' );
[demo_shortcode][/demo_shortcode]
Output : <a href=”http://random.cat/i/QRp74.jpg“></a>
this replaces the href to the required url but also replaces the image.
thanx in advance.

How to check to see if a wordpress shortcode $atts is equal to something

I am trying to figure out how to figure out how to check to see if a shortcode attribute is equal something and than if it is set a variable.
I have an image shortcode with an attribute of float and I want to set a class to float right if the user enters in float="right" as an attribute but if not than do nothing.
add_shortcode( 'img', 'img_shortcode' );
function img_shortcode( $atts, $content = null ) {
$atts = shortcode_atts(
array(
'float' =>'',
), $atts);
$ImgFloat = '';
if(float attribute = right){
$ImgFloat = 'class="img-right"';
}
return '
<div class="img-shortcode">
<img '. $ImgFloat .' src="'. $content .'" />
</div>
';
}
Above is the shortcode, as you can see the If statement is where I'm having the trouble, I would like to figure out how to check to see if the attribute float is set to anything if it is set to right, than make $imgFloat variable equal to the float right class that I have set.
Change this:
if(float attribute = right){
$ImgFloat = 'class="img-right"';
}
To this:
if ( 'right' == $atts['float'] ) {
$ImgFloat = 'class="img-right"';
}
OK so I may have complicated the answer by flipping the conditional around but it's a good habit to get into :)
The important part is we're saying if $atts['float'], which is the value of float="" in the shortcode, is equal to right then add the class.
shortcode_atts(... at the top of the function is where $atts['float'] is given a default value. If the user doesn't enter float=".. then the value is a blank string.
shortcode_atts returns an array combining the parameters passed in with the defaults you specify. So the float attribute will be stored in $atts['float']

Strange code in an attribute creating a Google Maps shortcode

I'm creating a shortcode that outputs the javascript code to create a customized Google Map, like this:
[map w="600" h="400" style="full" z="16" marker="yes" infowindow="<h2>Title</h2>" address="New York"]
Here are some extracts of the code:
function gmap($atts) {
$atts = shortcode_atts(array(
[...]
'infowindow' => '',
[...]
'style' => ''
), $atts);
[...]
//infowindow
if($atts['infowindow'] != '')
{
$thiscontent = htmlspecialchars_decode($atts['infowindow']);
$returncode .= '
var contentString = \'' . $thiscontent . '\';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
';
}
[...]
return $returncode;
}
Everything is fine if in "infowindow" I only use text, but if I use markup, like <h2>Title</h2> what I get in the code is:
var contentString = '<br />
<h2>Title</h2>
<p>';
... with two newlines that mess up the js.
Can anybody help me?
Thanks a lot!
Looks like you're getting screwed over by WordPress's automatic paragraph formatting.
Option 1: Sometimes you can get around this by switching to the html tab of the editor, then removing any whitespace in your markup, but this also has a tendency to fall apart next time you edit the same page/post.
Option 2: Disable wpautop in your theme's functions.php:
remove_filter( 'the_content', 'wpautop' );
However, this might screw up your content elsewhere on your site.
Option 3. Use yet another plugin, to toggle wpautop on or off of specific pages: https://wordpress.org/plugins/toggle-wpautop/
Option 4. Add some more shortcode attributes, then apply the required html via PHP, so you don't have to deal with this!

How can the last word of an element be styled?

Given a dynamically loaded heading (using PHP/WordPress) is there a pure CSS method of styling the final word? For instance:
<h1 class='featured'> This is my page title</h1>
Becomes
<h1 class='featured'> This is my page <span id="finalWord">title</span></h1>
If using CSS is not viable, is exploding the content of the <h1> tag the suggested way to do this?
AFAIK there is not a :last-word pseudo class in CSS (although that would be cool). You could use JavaScript for something like this, but if you have access to the <h1> server-side I'd do it with PHP. It's going to be part of the source code and probably easier.
A simple algorithm would be to find the last space in the string with strrpos() and use substr() to piece it back together wrapped in <span>.
I guess a really crude way would be to create a way to all the words and put it in a PHP array. Then echo all the values, and if it's the last one then put the <span id="finalWord"> before and the </span> after.
EX:
Step 1: Create the array
$your_text = "Your text goes here";
$pieces = explode(" ", $your_text);
Now you have all your data in a array. Each word is in it's object.
echo "<h1 class='featured'>";
$the_count = count($pieces);
foreach ($your_text as $i => $value) {
$i = $i + 1;
if ($i == $the_count) { echo "<span id='finalWord'>"; }
echo $value;
if ($i == $the_count) { echo "</span>"; }
}
echo "</h1>";
So basically what this code does is count how many objects are in your array and will check if the object being displayed is the last one. If it is, it will put the correct ID on it.
I just typed this code out real quick, so there could be some errors.
Coulton
is there a pure CSS method of styling
the final word
No such method in principle. CSS cannot modify the DOM.
Take text of dom element, find last word using your own criteria for the "word", wrap it into span and set innerHTML by the result.
Sorry for the follow up on an ancient post, this is the one that came up in my google searches for "wrap the last word in a string with a span tag using php" so I figured this is where I would put the solution I came up with.
Using the above as a starting point, I created this function to accomplish what I needed:
function wrap_last($string, $wrap_start = '<span class="font-bold">', $wrap_finish = '</span>') {
$string_array = explode(' ', $string);
$count = count($string_array);
$new_array = array();
foreach ($string_array as $i => $value) {
$i = $i + 1;
$array_part = '';
if ($i == $count) {
$array_part .= $wrap_start;
}
$array_part .= $value;
if ($i == $count) {
$array_part .= $wrap_finish;
}
$new_array[] = $array_part;
}
$new_string = implode(' ', $new_array);
return $new_string;
}

Resources