Through PCRE verb (*SKIP)(*F)
.
<[^<>]*>(*SKIP)(*F)|[A-Z0-9._%+-]+@([A-Z0-9.-]+.[A-Z]{2,4}|[A-Z0-9.-]+)
DEMO
<[^<>]*>
matches all the tags and the following PCRE verb (*SKIP)(*F)
makes the match to fail completely. Then the regex engine tries to match the pattern which was at the right of |
symbol against the remaining string.
$re = "/<[^<>]*>(*SKIP)(*F)|[A-Z0-9._%+-]+@([A-Z0-9.-]+\.[A-Z]{2,4}|[A-Z0-9.-]+)/mi";
$str = "<div data="[email protected],[email protected]"><a href="[email protected]" > [email protected], <b>[email protected]</b> other text, [email protected], ,<i>[email protected]</i></a></div >
";
$subst = "[---]";
$result = preg_replace($re, $subst, $str);
echo $result;
Output:
<div data="[email protected],[email protected]"><a href="[email protected]" > [---], <b>[---]</b> other text, [---], ,<i>[---]</i></a></div >
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…