php如何从文本文件中获取整个字符?

时间:2015-07-08 05:05:14

标签: php arrays readfile

我有文本文件

name,name1
willhaveishere1
name,name2
willhaveishere2
name,name3
willhaveishere3

我想读它并像那样返回

$nn = name1
$ss = willhaveishere1

使用我的代码我只得到name1

我的代码是

$file1 = "file.txt";
$file = file($file1);
$count = count($file);
if($count > 0) {
$i = 1;
foreach($file as $row) {
$n = strstr($row, 'name,');
$cc = array("name,");
$dd   = array("");
$nn = str_replace($cc, $dd, $n);
echo $nn;
$i++; } }

3 个答案:

答案 0 :(得分:1)

这可能就是你需要的

if($count > 0) {
    foreach($file as $row) {
        $pos = strpos($row, ',');
        if($pos !== false){
            echo substr($row, $pos + 1);
            $nn[] = substr($row, $pos + 1);
        } else {
            echo $row;
            $ss[] = $row;
        }
    }
}

修改

是的,只需循环播放,但请确保$nn$ss具有相同的计数,具体取决于您的文件。

另请注意:mysql_*函数已被弃用,因此请改用mysqliPDO

$count = count($nn);
for($i=0; $i < $count; $i++){
    $sql = "INSERT INTO users(name, line) VALUES('$nn[$i]', '$ss[$i]')"; mysql_query($sql); 
}

编辑2

试试这个例子:

$file = array(
    0 => 'name,name1',
    1 => 'willhaveishere1',
    2 => 'name,name2',
    3 => 'willhaveishere2',
    4 => 'name,name3',
    5 => 'willhaveishere3'
);
$count = count($file);
if($count > 0) {
    foreach($file as $row) {
        $pos = strpos($row, ',');
        if($pos !== false){
            $nn[] = substr($row, $pos + 1);
        } else {
            $ss[] = $row;
        }
    }
}

echo '<pre>';
$count = count($nn);
for($i=0; $i < $count; $i++){
    $sql = "INSERT INTO users(name, line) VALUES('$nn[$i]', '$ss[$i]');"; 
    echo $sql.PHP_EOL;
}

答案 1 :(得分:0)

您可以尝试这种简单的方法:

if($fh = fopen("file.txt","r")){ 
    $nameBefore = false;

    //loop through every line of your file
    while (!feof($fh)){ 
        $line = fgets($fh);

        //check if the name was detected in previous line
        if ($nameBefore !== false)
        {
            //you have the set of name and line, do what you want
            echo $nameBefore . ': ' . $line . '<br />';
            $nameBefore = false;
        }
        else
        {
            //see if the line is made of two coma separated segments and the first one is 'name'
            //Remember the name for the next line
            $parts = explode(',', $line);
            if (count($parts) == 2 && $parts[0] == 'name')
                $nameBefore = $parts[1];
        }
    } 
    fclose($fh); 
} 

答案 2 :(得分:0)

一个选项是使用strpos查找行中第一次出现的字符,如果找到则从该位置前的行中删除所有内容。这样,您只剩下您感兴趣的部分线。

代码:

$character = ',';
$fileHandle = fopen('file.txt', 'r');
while (!feof($fileHandle)) {

    // Retrieve the line from the file
    $line = fgets($fileHandle);

    // If the line contains the character
    // Remove everything before the character
    $charPos = strpos($line, $character);
    if ($charPos !== false) {
        $line = substr($line, $charPos + 1);
    }

    // Do something with the remainder of the line
    echo $line . PHP_EOL;
}

fclose($fileHandle);

输出:

name1
willhaveishere1
name2
willhaveishere2
name3
willhaveishere3

如果您想要检索以下行,只需在循环中执行另一个检索行调用:

while (!feof($fileHandle)) {

    // Retrieve two lines in one loop iteration
    $lineOne = fgets($fileHandle);
    $lineTwo = fgets($fileHandle);
}

确保仅在第一行应用逗号替换部分。如果您的数据不一致,这可能会导致问题。

希望这有帮助。