PHP preg_replace help

Discussion in 'Coding Help' started by Tom, Apr 7, 2011.

Tags:
  1. Tom

    Tom New Member

    I need to parse a string using php preg_replace. I need it to find instances of src=" then change it to src="http://www.mysite.ie/ but only if it's not already src="http://www.mysite.ie/

    so it would change the following

    Code:
    <img src="http://www.irishwebmasterforum.com/images/0.png">
    <img src="http://www.mysite.ie/images/0.png">
    <img src="http://www.irishwebmasterforum.com/images/0.png">
    
    to

    Code:
    <img src="http://www.mysite.ie/images/0.png">
    <img src="http://www.mysite.ie/images/0.png">
    <img src="http://www.mysite.ie/images/0.png">
    
    Does anyone know how to do this? I hate regular expresions.


    Edit: Ok I'm getting somewhere now using the pattern below, this might do the trick..

    Code:
    (?!src="http)src="
     
  2. php.allstar

    php.allstar New Member

    PHP:
    <?php

        $source 
    '<img src="http://www.irishwebmasterforum.com/images/0.png">
    <img src="http://www.mysite.ie/images/0.png">
    <img src="http://www.irishwebmasterforum.com/images/0.png">'
    ;

        
    $data preg_replace('#<img src="http://[a-z.]+/images/0.png">#iSu''<img src="http://www.mysite.ie/images/0.png">'$source);
        
        echo 
    $data;

    ?>
     

Share This Page