php - 捕获每一行的第一个字

标签 php file-get-contents

我有一个文本文件,我想从中捕获每个第一个单词:

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/

相关文章:

javascript - 表单验证不验证

php - 我的 php 搜索引擎代码没有给我结果

php - CakePHP 错误 : Unable to configure the session, 设置 session.auto_start 失败

php - 在同一服务器上使用 file_get_contents 或 curl 获取 url

php - 无法访问 Facebook Graph API

php - 是否有替代 file_get_contents 的方法?

PHP mysql_fetch_assoc() 错误

php - 将输入数组形成到 MySQL DB

php - 我有一个带有 html div 内容的 php 脚本。需要使用包括 css 的 html 代码获取 php

php - 在 PHP 中使用 URL 获取元素的特定内容 block