preg_replace() substrings NOT IN tags
| From: | Bruce BrackBill | Date: | Tue, 31 Jul 2001 18:32:02 +0000 |
| Subject: | preg_replace() substrings NOT IN tags | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-60471@lists.php.net to get a copy of this message | ||
Hi,
I'm trying to figure out a regex that would match substrings in
a string that are NOT inside tags. In the example below I try
to replace the substring 'paris' with
<a href=\"http://www.somehost.com">paris</a> but it also matches
'paris' in the image name.
* this does not work:
$url = "http://www.somehost.com";
$needle = "paris";
$haystack = "<img src=\"/images/3_im_paris_tower.jpg\"> paris trip";
$regex="($needle).*?[^>]";
echo preg_replace ("/$regex/", "<a href=\"$url\">\\1</a> ",$haystack);
An even simpler example of what I am trying to do is as follows:
Match 'a' in the following string if it is followed by
( zero or more of *any* character, then NOT a '>' )
* this does not work ( using regex ASSERTIONS )
* it matches the 'a' before the '>' ... which I don't want )
$haystack = "ad>bc|abc ";
$regex="(a(?=.*?)(?!>))";
Thanks in advance for your help,
Bruce
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp