【问题标题】:Regex containing mailaddresses but not value=包含邮件地址但不包含值的正则表达式 =
【发布时间】:2015-12-23 04:52:51
【问题描述】:

我如何从 html 中捕获所有邮件地址,但忽略作为表单值的地址。例如:

    <p>Mail: anymail@example.com</p>
   ...
    <input value="anymail@example.com">
   ...
    <a href="mailto:anymail@example.com">Kontakt: <span>anymail@example.com</span></a>

我需要所有地址,但不是输入字段中的地址(它是一个表单值)。

为了匹配我的地址:

(mailto:|)[a-z0-9_\.\-\+]+@[a-z0-9\-\.]+\.[a-z]{2,}+)

【问题讨论】:

  • 您需要先用 DOMDocument 解析 HTML 以排除 value 属性。

标签: php regex regex-negation


【解决方案1】:

使用正则表达式解析 HTML 不是一个好主意。不过,更简单的方法是去除所有作为输入值的电子邮件,然后匹配所有提醒电子邮件。

这是一个使用正则表达式匹配电子邮件的示例。

$html = preg_replace("/value=[\"'][a-z0-9_\.\-\+]+@[a-z0-9\-\.]+\.[a-z]{2,}[\"']/", "", $html);

preg_match_all("/[a-z0-9_\.\-\+]+@[a-z0-9\-\.]+\.[a-z]{2,}/", $html, $matches);
var_dump($matches); //will output all emails but the one inside value.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 2022-07-21
    • 1970-01-01
    • 1970-01-01
    • 2019-03-23
    • 1970-01-01
    • 2017-01-26
    相关资源
    最近更新 更多