如何从html内部文本替换电子邮件地址 [英] how to replace email address from html innertext

查看:26
本文介绍了如何从html内部文本替换电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从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

推荐答案

通过PCRE谓词(*SKIP)(*F)

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

DEMO

<[^<>]*>匹配所有标记,下面的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 >
";
$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 >

这篇关于如何从html内部文本替换电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆