我有一个文本文件,我想从中捕获每个第一个单词:
First name|All|01.01.55.41
Second name||01.01.55.41
Third name||01.01.55.41
我正在尝试:
function get_content() {
$mailGeteld = NULL;
$mailGeteld = file_get_contents("content.txt");
$mailGeteld = explode("|",$mailGeteld);
return $mailGeteld[0];
}
但现在我只得到“名字”,我怎样才能循环它以便结果如下:
First name, Second name, Third name
最佳答案
file
逐行读取文件。
function get_content() {
$firstWords = array();
$file = file("content.txt"); //read file line by line
foreach ($file as $val) {
if (trim($val) != '') { //ignore empty lines
$expl = explode("|", $val);
$firstWords[] = $expl[0]; //add first word to the stack/array
}
}
return $firstWords; //return the stack of words - thx furas ;D
}
echo implode(', ', get_content()); //puts a comma and a blankspace between each collected word
关于php - 捕获每一行的第一个字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16926234/