Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 >

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...