Hello,

I am trying to replace an image inside an html file using this:

The file contains this:

<img class="uiProfilePhoto headerTinymanPhoto uiProfilePhotoLarge img" src="http://profile.ak.fbcdn.net/hprofile-ak-snc4/370282_100003830116243_1299179951_q.jpg" alt="" />

And I want to replace it with this:

<img class="uiProfilePhoto headerTinymanPhoto uiProfilePhotoLarge img" src="http://example.com/FC.jpg" alt="" />

This is what I have tried:

$URLSearchString = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";

$page_html = preg_replace("#headerTinymanPhoto uiProfilePhotoLarge img\" src=\"($URLSearchString)\"#", 'http://example.com/FC.jpg', $page_html);  

But its not working. 😕

Can anyone see what I have done wrong ?

Is preg_replace the correct tool to use ?

Thanks.

.

    It's quite likely that the correct tool would be a proper HTML parser, like the one built into [man]DOM[/man].

      $URLSearchString = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";
      
      $page_html = preg_replace("#headerTinymanPhoto uiProfilePhotoLarge img\" src=\"($URLSearchString)\"#", 'http://example.com/FC.jpg', $page_html);  
      
      

      If you want to use preg_replace, you might want to test your pattern first using [man]preg_match_all[/man] to make sure it actually matches what you want it to match. Once you are sure the pattern matches, then you can proceed to replace it with something.

      Also, "not working" isn't very helpful. You should get in the habit of describing specifically what failed.

        Write a Reply...