1
votes

I'm new to preg_replace and have a problem.

I have this kind of text

$text  = 'welcome to example.com site and this is image from example.com <img alt="image of example.com site" src="http://example.com/image.jpg"/> have a nice day!';

is it possible to make the result became

welcome to yyy.com site and this is image from yyy.com <img alt="image of yyy.com site" src="http://example.com/image.jpg"/> have a nice day!

like you see here, that on the plain text and alt example.com change to yyy.com BUT NOT on img source, the img src still goes to example.com (i want to keep it like that)

also if it possible make the replace is case insensitive so eXample.com examplE.com exAmPLe.com all replace to yyy.com

note: in some img, they have alt attribute but some of them doesn't have alt attribute

2
So you want to keep the image tag and the text after it?Jason McCreary
yes sir i want to keep the image source urlprabowo

2 Answers

0
votes

You can use lookbehinds:

preg_replace("/(?<!\/)xxx/", "yyy", $text);
0
votes

Just a note on ethics here.

If site example.com belongs to someone else, you should not claim ownership of that image (as belonging to your site example2.com). At best, this is "hotlinking"; at worst, theft. If you have permission to access the image from your site, fine. Note that anyone pulling up that image should see that it's coming from example.com and not example2.com, and ought to be suspicious as to why you're claiming it's on example2.com. Their security software may even block such cross-site activity.