I figured out what I was doing wrong. My regexp should of looked like this /<a[^>]*>(Tampa)<\/a>/ and that made it more specific and kept it to that match.

Jason Lehman 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.


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

Reply via email to