preg_replace

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
imanewbe
Forum Newbie
Posts: 3
Joined: Mon Feb 07, 2011 12:12 am

preg_replace

Post 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?
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: preg_replace

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: preg_replace

Post by John Cartwright »

Moved to Regex.
Goldwinterrain
Forum Newbie
Posts: 4
Joined: Sun Mar 13, 2011 9:15 pm

Re: preg_replace

Post 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.
Post Reply