我正在嘗試抓取一封電子郵件。假設我有一個很長的字符串:$str = "this is the email [email protected] which you might want to scrape";$needle_1 = "@";$needle_2 = array("[ ]","[,]","["]");我想提取電子郵件。我認為使用@是解決方案。 所以基本上,我需要獲取@之前和之后的所有字符,直到空格、逗號或"。目前最好的試用是:$string = 'this is the email [email protected] which you might want to scrape';$template = "/[\w]+[@][\w]+[.][\w]+/";preg_match_all($template, $string, $matches);var_dump($matches);我怎樣才能做到這一點?
2 回答

千巷貓影
TA貢獻1829條經驗 獲得超7個贊
我認為它看起來更好
$string = 'this is the email [email protected] which you might want to scrap';
$template = "/[\w]+[@][\w]+[.][\w]+/";
preg_match_all($template, $string, $matches);
var_dump($matches);

鴻蒙傳說
TA貢獻1865條經驗 獲得超7個贊
您應該為此創建一個正則表達式:
<?php
$str = "this is the email [email protected] 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 關注
- 190 瀏覽
添加回答
舉報
0/150
提交
取消