I wish to replace all src
attributes of img
tags in a string.
My string:
$string = "some text with <img src='/uploads/images/5d0554.jpeg'> and
<img src='/uploads/images/507a.jpeg'> or <img src='/uploads/images/0a74.jpeg'> in it.";
should become:
$string = "some text with <img src='some/value/one.jpeg'> and
<img src='alue/SomethingElse.png'> or <img src='Value3'> in it.";
What I tried to do:
$regex = '/< *img[^>]*src *= *["\']?([^"\']*)/i';
preg_match_all($regex, $string, $matches);
$srcMatches = $matches[1];
$replacementValues = ["some/value/one.jpeg", "value/SomethingElse.png", "Value3"];
preg_replace_callback($regex, function($matches) use (&$replacementValues) {
return array_shift($replacementValues);
}, $string);
This gives me
some long text with some/value/one.jpeg'> and
value/SomethingElse.png'> or Value3'> in it.
I also tried with preg_replace
, but that gave me issues because of all the /
's in the values to be replaced.