0

I have a string with more than 1000+ symbols, and I need to find substring, cut it from curret position and put into another pisition on same string.

P.S. Sorry for my bad english.

//I have
$string = "< p>< img class='classes' src='https://example.com/img/img.jpg' atl='img'>< /p>";

// I need
$string = "< p>< img class='classes' data-src='https://example.com/img/img.jpg' src=' ' atl='img'>< /p>";

I need find in $string src, cut the link and place it to same but in data-src

u_mulder
  • 54,101
  • 5
  • 48
  • 64

1 Answers1

0

You can use regex by using the preg_replace_callback function like:

$string = "<p><img class='classes' src='https://example.com/img/img.jpg' atl='img' /></p>";

preg_replace_callback('#\s+src(\s+)?=(\s+)?(\'|")([^\'"]+)(\'|")#', function($m){
    return ' data-src="'.$m[4].'" src=""';
}, $string);

echo $string;
Kubwimana Adrien
  • 2,463
  • 2
  • 8
  • 11