Results 1 to 4 of 4

Thread: preg_replace problem [resolved]

  1. #1

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141

    preg_replace problem [resolved]

    I am such a regex noob it is not even funny. I have found this function to covert BBcode to HTML, but I found a problem.
    If I do this, then it makes the URL like it should and puts the words 'Other Text' after the link like you would exprect.
    Code:
    [ url=http://blah.com]balh[/url] Other Text
    '<a href="http://blah.com target="_blank">blah</a> Other text"
    If I do this, the 2 links get merged together.
    Code:
    [ url=http://blah.com]balh[/url] Other Text [ url=http://blah2.com]blah2[/url]
    '<a href="http://blah.com" target="_blank">balh[/url] Other text [ url=http://blah2.com]blah2</a>
    Here is my code to do the regex stuff
    PHP Code:
    $texte preg_replace('/\[url=(.+?)\](.+)\[\/url\]/i','<a href="\\1" target="_blank">\\2</a>',$texte); 
    **edit, I had to add the space between [ and url because vbforums was making it a link

    Crap, it messes up my preg_replace stuff too.
    Last edited by blindlizard; Mar 25th, 2004 at 08:35 PM.
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629
    I use this, it works fine with multiple links on the same line:

    Code:
    <?
      $txt = preg_replace("/\[link=(.*?)\](.*?)\[\/link\]/i", "<a href=\"$1\" target=\"new_win\">$2</a>", $txt);
    ?>
    Like Archer? Check out some Sterling Archer quotes.

  3. #3

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    I'll give it a shot, thanks. Any idea what the difference is between (.+?) and (.*?)
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

  4. #4

    Thread Starter
    Frenzied Member blindlizard's Avatar
    Join Date
    Feb 2001
    Location
    Austin, TX - United States of America
    Posts
    1,141
    It works!!!! You rock! Maybe some day I will try to understand regular expressions
    I drink to make other people more interesting!
    [vbcode]On Error GoTo Bar[/vbcode]
    http://www.monsterlizard.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width