Change to preg_replace

Discussion in 'PHP' started by adamjblakey, Jun 11, 2014.

  1. #1
    Hi,

    I have the following function which changes a URL in my string. Its working fine but i want to stop it changing the image name also. E.g.

    Here is an example string.

    <h1>Welcome to website</h1>    <p><strong><u>My title</u></strong><img src="http://ablered.mysite.info/uploads/images/thumbs/ablered.mysite.info_--_532966935.PNG" width="250" align="right" style="margin: 0px 0px 0px 25px;"></p>
    Code (markup):
    As you can see the image contains the same domain in the image name. I only want the domain changing in the link but not the image. My function changes them both but i cant see how to do just the URL.

    This is the function.

    function formatUrlsInText($text, $newURL)
    {
        return preg_replace( '/[a-z0-9]+\.mysite\.info/i' , $newURL, $text);
    }
    Code (markup):
     
    adamjblakey, Jun 11, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Can't you just make it match the url after
    http://?
    Code (markup):
    so change the function to something like this:
    
    function formatUrlsInText($text, $newURL)
    {
      return preg_replace( '/http:\/\/[a-z0-9]+\.mysite\.info/i' , $newURL, $text);
    }
    
    PHP:
    Point: this means you'll need to have
    http://
    Code (markup):
    in front of replacement url
     
    PoPSiCLe, Jun 11, 2014 IP
  3. adamjblakey

    adamjblakey Active Member

    Messages:
    1,121
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Thats brilliant thank you very much, works perfectly.
     
    adamjblakey, Jun 12, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Thanks - as always, I always recommend chosing "Best solution" within the thread (my post, basically) to help others quickly get to the answer, and also to gain me a "best answer" :D
     
    PoPSiCLe, Jun 13, 2014 IP