Page 1 of 1

preg_replace

Posted: Thu Feb 10, 2011 7:07 pm
by imanewbe
Hey

I am using an html editor and when displaying the after it has been submited I use preg_replace to get the urls showing up correctly.
Currently this is not working if I have multiple urls in the same paragraph. If there are multiple urls then the url contains all text between the two urls for example

this is the first one url1 and this is the text between url2 and this is the end of the paragraph

this would show up as:
this is the first one URL and this is the end of the paragraph.

this is how i am using the function

Code: Select all

$page["content_parsed"] = preg_replace("/\[url=(.*)\](.*)\[\/url\]/i", "<a href=\"\\1\">\\2</a>", $page["content_parsed"]);
Any ideas on how I'd go about fixing this?

Re: preg_replace

Posted: Fri Feb 11, 2011 1:34 pm
by tr0gd0rr
You need to specify ungreedy by adding a `?` after each of the asterisks in your expression. Otherwise it is looking for the largest possible match--which in this case includes text from the rest of the paragraph.

Re: preg_replace

Posted: Fri Feb 11, 2011 1:40 pm
by John Cartwright
Moved to Regex.

Re: preg_replace

Posted: Sun Mar 13, 2011 10:01 pm
by Goldwinterrain
I am using an html editor and when displaying the after it has been submited I use preg_replace to get the urls showing up correctly.
Currently this is not working if I have multiple urls in the same paragraph. If there are multiple urls then the url contains all text between the two urls for example

this is the first one url1 and this is the text between url2 and this is the end of the paragraphYou need to specify ungreedy by adding a `?` after each of the asterisks in your expression. Otherwise it is looking for the largest possible match--which in this case includes text from the rest of the paragraph.