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