1

循环通过 php 中的文本文件,我使用 preg_match 来检测该行是否包含“default”,并在该单词后放置一个逗号而不是空格,但它不起作用:

   $FSCS = "";

 //Read the txt file
if(($handle = fopen("FSCS.txt", "r")) != false)
{
//Loop through each line
  while(($data = fgetcsv($handle, 1000, ",")) != false)
  {
    if(preg_match("/default/", $data[0])) $FSCS .= str_replace("default ", "default,", trim($data[0]))."\n";        

    else $FSCS .= trim($data[0]).",";
  }
}

每一行都由“else”语句处理

4

1 回答 1

0
$FSCS = "";

//Read the txt file
if (($handle = fopen("FSCS.txt", "r")) != false)
{
    //Loop through each line
    // Use fgets to read the whole line and use fgetcsv to read and parse a CSV file
    while(($data = fgets($handle, 1000)) != false)
    {
        // The \s matches whitespace
        if (preg_match("/default\s/", $data))
        {
            $FSCS .= preg_replace("/default\s/", "default,", $data) . "\n";        
        }
        else
        {
            $FSCS .= $data . "\n";
        }
    }
}
于 2013-07-26T11:02:25.383 回答