Regular Expression Help in my PHP! Sorry if wrong group
| From: | Jason Lehman | Date: | Wed, 15 Jan 2003 17:53:53 +0000 |
| Subject: | Regular Expression Help in my PHP! Sorry if wrong group | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-131726@lists.php.net to get a copy of this message | ||
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.