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

I have text file

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

i want read it and return like that

$nn = name1
$ss = willhaveishere1

with my code i get only name1

my code is

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

This is probably what you need

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

EDIT

Yes, just loop through, but make sure both $nn and $ss has same count, which is depending on your file.

Also Note: mysql_* functions has been deprecated, so please use mysqli or PDO instead

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

EDIT 2

try this example:

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

You can try this straightforward method:

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

One option is to use strpos to find the first occurrence of the character in the line, and if found remove everything from the line before that position. This way you are left with only the part of the line you are interested in.

Code:

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

Output:

name1
willhaveishere1
name2
willhaveishere2
name3
willhaveishere3

If you wish to retrieve the following line, simply do another retrieve line call in your loop:

while (!feof($fileHandle)) {

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

Making sure to only apply the comma replace part on the first line. This can lead to problems though if your data is... inconsistent.

Hope this helps.