php 如何从文本文件中获取特定字符后的整行?

php how to get whole line after spicific character from text file?

我有文本文件

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

我想读它 return 那样

$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++; } }

您可以试试这个简单的方法:

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); 
} 

这可能就是您需要的

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_* functions has been deprecated, so please use mysqli or PDO 而不是

$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;
}

一个选项是使用 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);
}

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

希望这对您有所帮助。