Search code examples
phpreplace

replace a string with double quotes and a pattern using str_replace


i want to replace src="images/imagename.jpg" to src="http://xyz.com/images/imagename.jpg" i tried doing with str_replace. But this does seems to have issue with double quotes. Any easy solutions for this?

thank you


Solution

  • Try using \ escape characters on the double quotes -- like this: \"

    return str_replace("src=\"images/imagename.jpg\"", "src=\"http://xyz.com/images/imagename.jpg\"", $string);
    

    With a string passed through htmlentities:

    return str_replace("src="images/imagename.jpg"", "src="http://xyz.com/images/imagename.jpg"", $string);