Regexp for extracting a mailto: address

后端 未结 5 837
醉梦人生
醉梦人生 2020-12-12 02:29

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

....
5条回答
  •  旧时难觅i
    2020-12-12 03:23

    If you want to match the whole thing from :

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

    To fastern and shortern it:

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

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

    Example:

    $html ='';
    
    $r = '`\]+)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) "test@live.com"
        [3]=>
        string(0) ""
        [4]=>
        string(4) "test"
      }
    }
    

提交回复
热议问题