【问题标题】:how to replace email address from html innertext如何从html内部文本替换电子邮件地址
【发布时间】:2014-12-02 15:15:01
【问题描述】:

我有一个问题,从 html 内部文本替换电子邮件地址。

我可以替换所有电子邮件地址。但我不能只替换特定的(html 的内部文本)。请帮帮我..

我试过preg_replace('/[A-Z0-9._%+-]+@([A-Z0-9.-]+\.[A-Z]{2,4}|[A-Z0-9.-]+)/iu','[---]',$data)

请帮助我。谢谢...

我的意见

<div  data="example1@dom.com,example4@dom.com"><a href="example1@dom.com" > example4@dom.com,  <b>example3@dom.com</b>  other text, example7@dom.com, ,<i>example5@dom.com</i></a></div >

预期输出:

<div  data="example1@dom.com,example4@dom.com"><a href="example1@dom.com" > [--],  <b>[--]</b>  other text, [--] ,<i>[--]</i></a></div >

live demo

【问题讨论】:

    标签: php regex preg-replace


    【解决方案1】:

    通过 PCRE 动词 (*SKIP)(*F).

    <[^<>]*>(*SKIP)(*F)|[A-Z0-9._%+-]+@([A-Z0-9.-]+\.[A-Z]{2,4}|[A-Z0-9.-]+)
    

    DEMO

    &lt;[^&lt;&gt;]*&gt; 匹配所有标签,并且以下 PCRE 动词 (*SKIP)(*F) 使匹配完全失败。然后正则表达式引擎尝试将| 符号右侧的模式与剩余的字符串进行匹配。

    $re = "/<[^<>]*>(*SKIP)(*F)|[A-Z0-9._%+-]+@([A-Z0-9.-]+\\.[A-Z]{2,4}|[A-Z0-9.-]+)/mi";
    $str = "<div data=\"example1@dom.com,example4@dom.com\"><a href=\"example1@dom.com\" > example4@dom.com, <b>example3@dom.com</b> other text, example7@dom.com, ,<i>example5@dom.com</i></a></div >\n";
    $subst = "[---]";
    $result = preg_replace($re, $subst, $str);
    echo $result;
    

    输出:

    <div data="example1@dom.com,example4@dom.com"><a href="example1@dom.com" > [---], <b>[---]</b> other text, [---], ,<i>[---]</i></a></div >
    

    【讨论】:

    • 非常感谢,我可以用另一个匹配的模式 ([0-9]{6,}) 替换,比如电话号码、skype 之后的特殊词:any_word 等
    • 是的,您可以将电子邮件 ID 模式替换为其他模式。
    • 我可以通过使用类似模式&lt;[^&lt;&gt;]*&gt;(*SKIP)(*F)|(m:)[0-9\+\s\(\)\,]{3,} 来得到这个输出M:[---]sdsd M:[---]sdfsd M:[---] 输入M: &lt;any_tag&gt;+48 668 157 750&lt;/any_tag&gt; sdsd &lt;b&gt;M:&lt;/b&gt; +48(22) 722 65 50 [demo](http://regex101.com/r/yR3mM3/12) sdfsd M: +48(22)&lt;any_tag&gt;247&lt;/any_tag&gt; 81 34 .. 请帮助我这个高级正则表达式。
    • 你能解释一下你想做什么吗?
    • see demo。那么你就可以理解了。我的意思是必须隐藏一些手机,传真号码,如m:fax:。但是这个输入是混合的html文本,就像以前的评论一样..
    【解决方案2】:
    [A-Z0-9._%+-]+@([A-Z0-9.-]+\.[A-Z]{2,4}(?![^<]*>)|[A-Z0-9.-]+)(?![^<]*>)
    

    试试这个。查看演示。

    http://regex101.com/r/yR3mM3/6

    $re = "/[A-Z0-9._%+-]+@([A-Z0-9.-]+\\.[A-Z]{2,4}(?![^<]*>)|[A-Z0-9.-]+)(?![^<]*>)/mi";
    $str = "<div data=\"example1@dom.com,example4@dom.com\"><a href=\"example1@dom.com\" > example4@dom.com, <b>example3@dom.com</b> other text, example7@dom.com, ,<i>example5@dom.com</i></a></div >";
    $subst = "[---]";
    
    $result = preg_replace($re, $subst, $str);
    

    输出:&lt;div data="example1@dom.com,example4@dom.com"&gt;&lt;a href="example1@dom.com" &gt; [---], &lt;b&gt;[---]&lt;/b&gt; other text, [---], ,&lt;i&gt;[---]&lt;/i&gt;&lt;/a&gt;&lt;/div &gt;

    【讨论】:

    • 谢谢,我可以用另一个匹配的模式替换,比如phone number , special word after skype: any_word
    • @AhosanKarimAsik 是 $subst 。你可以给它任何你想替换的值
    • 不,我说另一种模式[0-9]{6,} 像电子邮件模式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    相关资源
    最近更新 更多