Ahosan Karim Asik
Ahosan Karim Asik

Reputation: 3299

how to replace email address from html innertext

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 >

live demo

Upvotes: 0

Views: 140

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174756

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 >\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

vks
vks

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

Related Questions