PDA

Click to See Complete Forum and Search --> : preg_replace problem [resolved]


blindlizard
Mar 25th, 2004, 05:56 PM
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.[ 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.
[ 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
$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.

kows
Mar 25th, 2004, 07:30 PM
I use this, it works fine with multiple links on the same line:

<?
$txt = preg_replace("/\[link=(.*?)\](.*?)\[\/link\]/i", "<a href=\"$1\" target=\"new_win\">$2</a>", $txt);
?>

blindlizard
Mar 25th, 2004, 07:32 PM
I'll give it a shot, thanks. Any idea what the difference is between (.+?) and (.*?)

blindlizard
Mar 25th, 2004, 07:34 PM
It works!!!! You rock! Maybe some day I will try to understand regular expressions :)