Page 1 of 1

[SOLVED]Small quote problem

Posted: Thu Jun 02, 2005 1:18 pm
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

Posted: Fri Jun 03, 2005 2:35 am
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

Re: [SOLVED]Small quote problem

Posted: Fri Jan 23, 2009 4:43 pm
by jason.carter
what was the solution?