正则表达式包含mailaddresses但不是value =

时间:2015-09-25 11:04:52

标签: php regex regex-negation

如何从html中捕获所有mailaddresses但忽略表单值的地址。例如:

...

<div id="bgswitcher"></div>

<script type="text/javascript">

    var srcBgArray = ['@Url.Content("~/Content/galleries/bar/im1.png")', '@Url.Content("~/Content/galleries/bar/im2.png")', '@Url.Content("~/Content/galleries/bar/im3.png")'];

    $(document).ready(function () {
        $('#bgswitcher').bcatBGSwitcher({
            urls: srcBgArray,
            alt: 'Full screen background image',
            links: true,
            prevnext: true
        });
    });

</script>

...

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

要匹配我的地址:

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

1 个答案:

答案 0 :(得分:0)

使用正则表达式解析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.