note 54816 deleted from reference.pcre.pattern.syntax by colder
| From: | colder@php.net | Date: | Mon, 18 Jun 2007 18:03:51 +0000 |
| Subject: | note 54816 deleted from reference.pcre.pattern.syntax by colder | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-127562@lists.php.net to get a copy of this message | ||
Note Submitter: wfinn at yakasha dot net
----
Some simple assertions so you only match inside (or outside) an html tag:
<?php
$string1 = "<b><span class=\"match1\">match2</span><span
class=\"match3\">match4</span></b>match5";
$string2 = preg_replace( "/match(?![^<]*?>)/", "replacement", $string1 );
// Matches outside the html tag
$string3 = preg_replace( "/match(?=[^<]*?>)/", "replacement", $string1 );
// Matches inside the html tag
echo "1: " . str_replace(array("<",">"),
array("<",">"), $string1) . "<br>\n";
echo "2: " . str_replace(array("<",">"),
array("<",">"), $string2) . "<br>\n";
echo "3: " . str_replace(array("<",">"),
array("<",">"), $string3) . "<br>\n";
?>
This outputs:
1: <b><span class="match1">match2</span><span
class="match3">match4</span></b>match5
2: <b><span class="match1">replacement2</span><span
class="match3">replacement4</span></b>replacement5
3: <b><span class="replacement1">match2</span><span
class="replacement3">match4</span></b>match5