【问题标题】:find and replace a specific string of a a particular line in txt file with php用php查找并替换txt文件中特定行的特定字符串
【发布时间】:2014-04-23 15:23:20
【问题描述】:

我想知道如何用php将特定行的特定单词/字符串替换为文本文件。

文本文件内容如下:

 1|1|1
 nikki|nikki@yahoo.com|nikki
 nikki|nikki@gmail.com|nikki
 nikki|nikki@hotmail.com|nikki

领域详情:

 COLUMN:0 = $name,
 COLUMN:1 = $email,
 COLUMN:2 = $nickname,

更换细节:

 COLUMN:0 = $newName,
 COLUMN:1 = $newEmail,
 COLUMN:2 = $newnickName,     

从上面的内容你可以猜到查找/搜索是基于column:1。 Ans 如果找到匹配项,则替换 column:0 OR column:2 [基于选择]。

我尝试了[查找列:1]:

 $fileData = file("file.txt");
 foreach($fileData as $Key => $Val) { 
  $Data[$Key] = explode("|", $Val);
  if ( trim($Data[$Key][1]) == $email ){
unset($fileData[$Key]);
    //REPLACE TAKE PLACE HERE
    break;
  }
 }

[用于替换]:

 /* REPLACE NAME */
 $file = "file.txt";
 $oname = "|$name|";$nname = "|$newName|";
 file_put_contents($file, str_replace($oname, $nname, file_get_contents($file)));
 /* REPLACE NICKNAME */
 $file = "file.txt";
 $onickname = "|$nickname|";$nnickname = "|$newnickname|";
 file_put_contents($file, str_replace($onickname, $nnickname, file_get_contents($file)));

但它正在替换所有匹配的“$name”。

我也尝试了以下方式:

 $fileData[$Key] = str_replace($name, $newName, $fileData[$Key]);
 file_put_contents($file,$fileData);

/* $name & $newName -:> $nickname & $newnickname

但它不起作用。

如果我想用“nikkigmail”替换“nikki@gmail.com”的列:0 [“nikki”],那么数据应该是:

 1|1|1
 nikki|nikki@yahoo.com|nikki
 nikkigmail|nikki@gmail.com|nikki
 nikki|nikki@hotmail.com|nikki

并且,如果想用“hotmail”替换“nikki@hotmail.com”的column:2 [“nikki”],那么:

 1|1|1
 nikki|nikki@yahoo.com|nikki
 nikkigmail|nikki@gmail.com|nikki
 nikki|nikki@hotmail.com|hotmail

我可以得到要更正的代码吗?

【问题讨论】:

  • 你能发布你的预期输出吗?
  • @Shankar:感谢您的回复。我已经发布了预期的代码。 REPLACE CODE 1 效果很好,但这会替换文本文件中所有匹配的 $name,而不仅仅是 column:0 或 column:2。我只想替换 column:0 OR column:2 字符串,基于 column:1
  • 我没有询问代码。我的意思是您希望文本文件的最终输出如何。 (替换后)
  • 你试过这个步骤吗? 1:以读取方式打开文件。 2.替换所需位置的内容。 3.然后以写入模式打开文件。 4.然后写替换的内容。
  • LIKE:如果我想用“nikkigmail”替换“nikki@gmail.com”的第1列,那么nikki|nikki@yahoo.com|nikki
    nikkigmail|nikki@gmail .com|nikki
    nikki|nikki@hotmail.com|nikki

标签: php html text web replace


【解决方案1】:

这是我将如何替换此类内容的方法。与其担心str_replace,不如实际修改file返回的数组?

<?php

$email = "nikki@gmail.com"; // Search email
$data = file("file.txt", FILE_IGNORE_NEW_LINES); // Read in the data

foreach($data as $key => $line) {
    $bits = explode("|", $line);
    if($bits[1] === $email) {
        // Update this in place,
        $bits[0] = "nikkigmail";
        $data[$key] = implode("|", $bits);
    }
}

$write = implode("\n", $data); // the data to write however you please.

请记住,这也可以扩展以满足您的行/列需求。例如,您可以使用类似的东西。

/**
 * The reason these are named differently is because they don't always
 * search/replace. For example, you can find nikki@gmail.com in one row,
 * but just be setting a different column in that row to a value..
 */

$match = array('col' => 1, 'str' => 'nikki@gmail.com'); // Search data at row
$update = array('col' => 0, 'str' => 'nikkigmail'); // Replace data at row

$data = file("file.txt", FILE_IGNORE_NEW_LINES); // Read in the data

foreach($data as $key => $line) {
    $bits = explode("|", $line);
    if($bits[$match['col']] === $match['str']) {
        // Update this in place,
        $bits[$update['col']] = $update['str'];
        $data[$key] = implode("|", $bits);
    }
}

$write = implode("\n", $data); // the data to write however you please.

【讨论】:

  • 这正是这样做的方法。 OP的问题是不使用任何形式的搜索和替换;它隐含地将数据视为二维数组(其中文件中的行是行,管道创建列)。
  • @Tristan : 嗯... lukin 一段不错的代码 :) 让我试试这个
  • 请记住,我还没有在数据上实际测试过这个,你可能不得不摆弄它,如果你有任何问题,请告诉我,我可以看看。
  • 它实际上并没有写入任何数据,我把这部分留给你做。末尾的变量$write 包含您需要编写的所有文件文本内容。
  • 我使用了"$fp = fopen("text.txt", "a") or die("Couldn't open $file for writing!_success"); fwrite($fp,implode( "\n", $data)) 或 die("无法将值写入文件!_success"); fclose($data);"代替“$write”。而这段代码只是复制文件内容并将它们添加到文件末尾的一行中,当使用“w”[write]时,只需复制文件并将其替换为单个文件行
猜你喜欢
  • 2018-02-21
  • 2017-01-16
  • 1970-01-01
  • 2015-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-13
相关资源
最近更新 更多