我有以下类型的txt格式的数据,下面有数百行。如何只取他们的电子邮件。
email1@yahoo.com,US,Wolterman,http://www.example.com/profile.php?id=12
email2@yahoo.com,UK,superman,http://www.example.com/profile.php?id=21
发布于 2014-10-03 21:57:55
如果地址总是第一位的话,这里有一种方法可以做到这一点。
$text = <<<DATA
email1@yahoo.com,US,Wolterman,http://www.example.com/profile.php?id=12
email2@yahoo.com,UK,superman,http://www.example.com/profile.php?id=21
email3@yahoo.com,US,Wolterman,http://www.example.com/profile.php?id=12
email4@yahoo.com,UK,superman,http://www.example.com/profile.php?id=21
email5@yahoo.com,US,Wolterman,http://www.example.com/profile.php?id=12
email6@yahoo.com,UK,superman,http://www.example.com/profile.php?id=21
DATA;
preg_match_all('~^[^,]+~m', $text, $matches);
echo implode("\n", $matches[0]);
输出
email1@yahoo.com
email2@yahoo.com
email3@yahoo.com
email4@yahoo.com
email5@yahoo.com
email6@yahoo.com
发布于 2014-10-03 22:08:08
如果您的文件在文本文件中,并且每一行都在一行,那么您可以提取每一行并收到电子邮件.
$array = array(); // Array where emails are stored
$handle = fopen("textfile.txt", "r");
if ($handle) {
while (($line = fgets($handle)) !== false) {
$array[] = explode(",",$line)[0]; // stores email in the array
}
} else {
// error opening the file.
}
fclose($handle);
print_r($array);
发布于 2014-10-03 21:54:04
试试explode()
$str = 'email1@yahoo.com,US,Wolterman,http://www.example.com/profile.php?id=12';
$res = explode(',', $str);
echo $res[0]; //email1@yahoo.com
https://stackoverflow.com/questions/26190110
复制