preg_replace help!!!

Discussion in 'PHP' started by yo-yo, Jul 25, 2005.

  1. #1
    I need help with this :) ... I have a database with 3,000 records in it and every row includes a field with some text and urls inside it. The url and number of url's on every record varies...

    Heres an example of the code...

    some random text here
    <a href="http://www.some-url.com/randomtexthere.html?randomnumbers">random text here</a> <br><br>
    some random text here
    <a href="http://www.some-url.com/randomtexthere.html?randomnumbers">random text here</a><br>
    blah blah more text
    HTML:
    What i need to do.... is grab every url and change it to a variable (lets say $newurl) of my own. so the code should change to this;

    some random text here
    <a href="$newurl">random text here</a> <br><br>
    some random text here
    <a href="$newurl">random text here</a><br>
    blah blah more text
    HTML:

    From what i've read, the preg_replace function will do what i need, however i can't figure out how to write one of these "mixed patterns" for it to search for :confused: , can anyone give me a hand :)
     
    yo-yo, Jul 25, 2005 IP
  2. UndiesHosting

    UndiesHosting Active Member

    Messages:
    219
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    78
    #2
    Unless all the URLs end in something common, your best bet is to match the quotes and everything inside

    preg_replace('/\"http.*\"/','"$newurl"', $string_of_code);
    Code (markup):
     
    UndiesHosting, Jul 26, 2005 IP
  3. anjanesh

    anjanesh Well-Known Member

    Messages:
    344
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    165
    #3
    
    preg_replace("/(href=\")(.*?)(\")/i","\\$1$newurl\\$3",$string_of_code)
    
    Code (markup):
     
    anjanesh, Jul 28, 2005 IP