Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

php - Regexp for extracting a mailto: address

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

<a href="mailto:[email protected]">....</a>

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to match the whole thing from :

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

To fastern and shortern it:

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

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

Example:

$html ='<div><a href="mailto:[email protected]">test</a></div>';

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

Output:

array(1) {
  [0]=>
  array(5) {
    [0]=>
    string(39) "test"
    [1]=>
    string(1) " "
    [2]=>
    string(13) "[email protected]"
    [3]=>
    string(0) ""
    [4]=>
    string(4) "test"
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...