Thanks for the response. I found this web page (http://www.itworld.com/nl/perl/01112001/) right after I submitted my question. It was great for explaining regexp's greediness.

1lt John W. Holmes wrote:
I have a script that turns certain words into links.  That I am having
no problems with, it is when I want to turn the links back in to plain
text that I am having the problem.  Below are my examples.  And I know
my regex is being greedy but I don't know how to stop it from being so
damn greedy I thought the ? mark would help but it didn't. Any help
would be appreciated. So here it goes:


The code:

$strStr = "This is <a href=test>USF</a> at <a href=test>Tampa</a>.  This
is <a href=test>USF</a> at <a href=test>Tampa</a>.";
echo $strStr."<br>\n";
$strStr = preg_replace("/<a.*?>(Tampa)<\/a>/","\\1",$strStr);
echo $strStr."<br>\n";
$strStr = preg_replace("/<a.*?>(USF)<\/a>/","\\1",$strStr);
echo $strStr."<br>\n";

The output:

This is <a href=test>USF</a> at <a href=test>Tampa</a>. This is <a
href=test>USF</a> at <a href=test>Tampa</a>.
This is Tampa. This is Tampa.
This is Tampa. This is Tampa.

The expected output:

This is <a href=test>USF</a> at <a href=test>Tampa</a>. This is <a
href=test>USF</a> at <a href=test>Tampa</a>.
This is <a href=test>USF</a> at Tampa. This is <a href=test>USF</a> at
Tampa.
This is USF at Tampa. This is USF at Tampa.

Well, a /U at the end of your pattern will make it so the regex isn't
greedy. That may solve  your problem.

Or you could try matching "/<a[^>]+>(Tampa)<\/a>/"

which will match an <a followed by anything that's not a >, followed by a >.
So it essentially makes it ungreedy.

---John Holmes...


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to