Reputation: 3299
i have a problem, for replacing email address from html innertext.
i can replace all email address. but i can't replace only specific(innertext of html). please help me..
i have tried with preg_replace('/[A-Z0-9._%+-]+@([A-Z0-9.-]+\.[A-Z]{2,4}|[A-Z0-9.-]+)/iu','[---]',$data)
please help me. thanks...
my input
<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 >
expected output:
<div data="[email protected],[email protected]"><a href="[email protected]" > [--], <b>[--]</b> other text, [--] ,<i>[--]</i></a></div >
Upvotes: 0
Views: 140
Reputation: 174756
Through PCRE verb (*SKIP)(*F)
.
<[^<>]*>(*SKIP)(*F)|[A-Z0-9._%+-]+@([A-Z0-9.-]+\.[A-Z]{2,4}|[A-Z0-9.-]+)
<[^<>]*>
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 >\n";
$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 >
Upvotes: 1
Reputation: 67968
[A-Z0-9._%+-]+@([A-Z0-9.-]+\.[A-Z]{2,4}(?![^<]*>)|[A-Z0-9.-]+)(?![^<]*>)
Try this.See demo.
http://regex101.com/r/yR3mM3/6
$re = "/[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);
Output:<div data="[email protected],[email protected]"><a href="[email protected]" > [---], <b>[---]</b> other text, [---], ,<i>[---]</i></a></div >
Upvotes: 1