[SOLVED]Small quote problem

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

Moderator: General Moderators

Post Reply
someberry
Forum Contributor
Posts: 172
Joined: Mon Apr 11, 2005 5:16 am

[SOLVED]Small quote problem

Post by someberry »

Having a small regex problem, and im not sure why it happens. As you can see, I am using the $2 twice (Not sure if thats why its happening), the first time it outputs fine, however, the second time it comes up with a double quote at the end, is anyone familiar with this problem, or knows how to fix it?

Thanks.

Code: Select all

$string = '[img="dsfdsf"]';
preg_replace("/\[img=(\")?(.*)(\")?\]/i", "<a href=\"$2\" target=\"_blank\">$2</a>", $string);
Ouputs: <a href="dsfdsf" target="_blank">dsfdsf"</a>

Edit: solved
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Since ~someberry didn't say how he solved it, I'll deduce that what the problem was was this....

.* matches anything any number of times so it was even reading everything beyond " "] ". That's called greedy and to prevent it you need to use the combination .*? where the ? allows the regex engine to stop seraching for "." if the following character is matched.

Code: Select all

/\&#1111;img=(\&quote;)?(.*?)(\&quote;)?\]/i
jason.carter
Forum Commoner
Posts: 35
Joined: Sat Jan 10, 2009 10:05 am

Re: [SOLVED]Small quote problem

Post by jason.carter »

what was the solution?
Post Reply