我正在嘗試抓取一封電子郵件。假設(shè)我有一個很長的字符串:$str = "this is the email query@stackoverflow.com which you might want to scrape";$needle_1 = "@";$needle_2 = array("[ ]","[,]","["]");我想提取電子郵件。我認(rèn)為使用@是解決方案。 所以基本上,我需要獲取@之前和之后的所有字符,直到空格、逗號或"。目前最好的試用是:$string = 'this is the email query@stackoverflow.com which you might want to scrape';$template = "/[\w]+[@][\w]+[.][\w]+/";preg_match_all($template, $string, $matches);var_dump($matches);我怎樣才能做到這一點?
2 回答
千巷貓影
TA貢獻(xiàn)1829條經(jīng)驗 獲得超7個贊
我認(rèn)為它看起來更好
$string = 'this is the email query@stackoverflow.com which you might want to scrap';
$template = "/[\w]+[@][\w]+[.][\w]+/";
preg_match_all($template, $string, $matches);
var_dump($matches);
鴻蒙傳說
TA貢獻(xiàn)1865條經(jīng)驗 獲得超7個贊
您應(yīng)該為此創(chuàng)建一個正則表達(dá)式:
<?php
$str = "this is the email query@stackoverflow.com which you might want to scrap";
$pattern = '/[a-z0-9_\-\+\.]+@[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
preg_match_all($pattern, $str, $matches);
var_dump($matches[0]);
?>
- 2 回答
- 0 關(guān)注
- 236 瀏覽
添加回答
舉報
0/150
提交
取消
