用于提取mailto:地址的正则表达式 [英] Regexp for extracting a mailto: address

查看:175
本文介绍了用于提取mailto:地址的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个reg exp,它可以接受一个字符串块,并找到与以下格式匹配的字符串:

I'd like a reg exp which can take a block of string, and find the strings matching the format:

<a href="mailto:x@x.com">....</a>

对于所有与此格式匹配的字符串,它将提取出mailto:之后找到的电子邮件地址.有什么想法吗?

And for all strings which match this format, it will extract out the email address found after the mailto:. Any thoughts?

这是内部应用程序所需的,而不是垃圾邮件发送者的目的!

This is needed for an internal app and not for any spammer purposes!

推荐答案

如果您要匹配来自的整个内容:

If you want to match the whole thing from :

$r = '`\<a([^>]+)href\=\"mailto\:([^">]+)\"([^>]*)\>(.*?)\<\/a\>`ism';
preg_match_all($r,$html, $matches, PREG_SET_ORDER);

要加快和缩短它:

$r = '`\<a([^>]+)href\=\"mailto\:([^">]+)\"([^>]*)\>`ism';
preg_match_all($r,$html, $matches, PREG_SET_ORDER);

第二个匹配组将是它的任何电子邮件.

The 2nd matching group will be whatever email it is.

示例:

$html ='<div><a href="mailto:test@live.com">test</a></div>';

$r = '`\<a([^>]+)href\=\"mailto\:([^">]+)\"([^>]*)\>(.*?)\<\/a\>`ism';
preg_match_all($r,$html, $matches, PREG_SET_ORDER);
var_dump($matches);

输出:

array(1) {
  [0]=>
  array(5) {
    [0]=>
    string(39) "test"
    [1]=>
    string(1) " "
    [2]=>
    string(13) "test@live.com"
    [3]=>
    string(0) ""
    [4]=>
    string(4) "test"
  }
}

这篇关于用于提取mailto:地址的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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