Kumar
Kumar

Reputation: 1217

How to change src attribute with specific string in php

I am using simple html dom to do this thing, there are some img tags. Where I want to change some src with specific string. I want to change urls which contains http://localhost.com in given text to https://i0.wp.com/localhost.com

Example:

$data='<p><img class="alignnone wp-image-36109 size-full" src="https://localhost.com/wp-content/uploads/2014/10/WjhzQlNRaDJrYUUx_o_using-freedom-for-unlimited-in-app-purchases-android-.jpg" alt="WjhzQlNRaDJrYUUx_o_using-freedom-for-unlimited-in-app-purchases-android-" width="480" height="360"/></p>

';

I have used below code to search https://localhost.com, But how can I change it.

$html->find('img[src^=https://localhost.com/]');

Result from simple html dom:

It gives me that search value but I want to change the value with something. That I have already told.

I have also use this regex to do this work.

echo preg_replace("/(<img.*src=)[\"'](.*)[\"']/m",'\1"https://i0.wp.com/\2\"',$data);

but it gives me out like

<p><img class="alignnone wp-image-36109 size-full" src="https://i0.wp.com/https://localhost.com/wp-content/uploads/2014/10/WjhzQlNRaDJrYUUx_o_using-freedom-for-unlimited-in-app-purchases-android-.jpg" alt="WjhzQlNRaDJrYUUx_o_using-freedom-for-unlimited-in-app-purchases-android-" width="480" height="360\"/></p>

All the src is with https://i0.wp.com/ but in regex, I want to get this result:

Result from regex:

https://i0.wp.com/https://i0.wp.com/localhost.com/wp-content/uploads/2016/03/sd.png?resize=300%2C300

Want to get result:

https://i0.wp.com/i0.wp.com/localhost.com/wp-content/uploads/2016/03/sd.png?resize=300%2C300

Can someone give me clue to get this, And also can you give me your answer in regex. It will helpful for me. Hope you understand my problem, you can comment below for more information.

The most important thing is that If any one want to give this question a down vote, please do that But Please Please Comment below Why Did you do that, I am not a genius php developer like you, I just learn from my mistake

Upvotes: 1

Views: 185

Answers (1)

Jan
Jan

Reputation: 43169

There you go (this uses the far superior DOMDocument library with xpath and regular expressions):

<?php

$data='<p><img class="alignnone wp-image-36109 size-full" src="https://localhost.com/wp-content/uploads/2014/10/WjhzQlNRaDJrYUUx_o_using-freedom-for-unlimited-in-app-purchases-android-.jpg" alt="WjhzQlNRaDJrYUUx_o_using-freedom-for-unlimited-in-app-purchases-android-" width="480" height="360"/></p>';

$dom = new DOMDocument();
$dom->loadHTML($data);

$xpath = new DOMXPath($dom);

# filters images 
$needle = 'https://localhost.com';
$images = $xpath->query("//img[starts-with(@src, '$needle')]");

# split on positive lookahead
$regex = '~(?=localhost\.com)~';
foreach ($images as $image) {
    $parts = preg_split($regex, $image->getAttribute("src"));
    $newtarget = $parts[0] . "i0.wp.com/i0.wp.com/" . $parts[1];
    $image->setAttribute("src", $newtarget);
}

# just to show the result
echo $dom->saveHTML();
?>

And see a demo on ideone.com.

Upvotes: 1

Related Questions