Php 如何在文件中查找包含字符串的行并在该行末尾添加内容
我有一个文件要编辑,其中包含:Php 如何在文件中查找包含字符串的行并在该行末尾添加内容,php,find,Php,Find,我有一个文件要编辑,其中包含: Categories, Diamond,10,11, Coal,21,21, 如何在包含“菱形”的行末尾添加字符串? 我拥有的代码可以在文件末尾添加字符串,但不知道如何在specyfic行中添加该字符串: $function_Result = mysql_fetch_row($function_Ask, 0); $file_To_Edit = "item_Data.csv"; $opened_File = fopen($file_To_Edit, 'w') o
Categories,
Diamond,10,11,
Coal,21,21,
如何在包含“菱形”的行末尾添加字符串?
我拥有的代码可以在文件末尾添加字符串,但不知道如何在specyfic行中添加该字符串:
$function_Result = mysql_fetch_row($function_Ask, 0);
$file_To_Edit = "item_Data.csv";
$opened_File = fopen($file_To_Edit, 'w') or die("Error. Code:2 - Can not open file $file_To_Edit");
$string_Data = $function_Result[0] . ",";
fwrite($opened_File, $string_Data);
fclose($opened_File);
如果文件内容不是太大,我应该使用
preg\u replace
。
如果文件内容不是太大,我应该使用
preg\u replace
。
以前发布的所有解决方案在处理大文件时都可能失败。这里有一个可以处理任何大小的文件。(应添加一些检查文件是否可读写等)
以前发布的所有解决方案在处理大文件时都可能失败。这里有一个可以处理任何大小的文件。(应添加一些检查文件是否可读写等)
我将添加一个字符串:$function\u Result=mysql\u fetch\u row($function\u Ask,0)代码>我将添加一个字符串:$function\u Result=mysql\u fetch\u row($function\u Ask,0)代码>+1 Sods,我没有想到要使用正则表达式。您的解决方案比我的好得多文件会很大,包含80行很长的字符串(很长),如果不是几兆字节,这仍然很快这是什么$1
和$append
?对不起,忘了PCRE#U多行,第二行后面应该有一个m,se编辑的答案+1 Sods,我没有想到要用正则表达式。你的解决方案比我的好得多。文件会很大,包含80行很长的字符串(很长),如果不是几兆字节,这还是很快的。这是什么$1
和$append
?对不起,忘了PCRE\u多行,第二行后面应该有一个m,se编辑的回答感谢您用大型文件的示例补充我的答案如何为包含“菱形”的行预匹配,并在此处配置缓冲区以在包含“菱形”的行末尾写入字符串?感谢您用大型文件的示例补充我的答案如何为包含“菱形”的行预匹配并将缓冲区配置为在包含“菱形”的行的末尾写入字符串?为什么它会输出array(18){[0]=>string(13)“Categories”,[1]=>string(1)”[2]=>string(1)”“[3]=>string(1)”[4]=>string(1)”[5]=>string(1)”[6]=>string(1)”[7]=>string(1)”[8]=>string(1)”[9]=>string(1)”[10]=>string(1)”[11]=>string(1)“[12]=>string(1)“[13]=>string(1)”[14]=>string(1)“[15]=>string(1)“[16]=>string(30)”Diamond,10,11,583583583583582”[17]=>string(11)“Coal,21,21”
?对不起,忘了评论var\u dump($opened\u文件)代码>-它转储$opened\u文件
变量。修改了我的答案。为什么它会输出array(18){[0]=>string(13)”类别,[1]=>string(1)”[2]=>string(1)”[3]=>string(1)”[4]=>string(1)”[5]=>string(1)”[6]=>string(1)”[7]=>string(1)”[8]=>string(1)”[9]=>string(1)”[10]=>string(1)”[11]=>string(1)”[12]=>string(1)”[13]>[14] =>string(1)“[15]=>string(1)”[16]=>string(30)“钻石,10,11,583583583583582”[17]=>string(11)“煤炭,21,21,”}
?对不起,忘了注释var_dump($opened_File);
-它转储$opened_File
变量。修改了我的答案。
$content = file_get_contents('file.txt');
/* in case of unwanted \r */ $content = str_replace("\r", '', $content);
$content = preg_replace("#^(Diamond.*)$#m", '$1' . $append, $content);
file_put_contents('file.txt', $content);
<?php
$string_Data = '444555';
$file_To_Edit = "./11.csv";
$opened_File = file($file_To_Edit) or die("Error. Code:2 - Can not open file $file_To_Edit"); // Reads entire file into array of file lines
$diamond_lines = preg_grep('#^Diamond#', $opened_File); // Finds array with line started with 'Diamonds'
foreach(array_keys($diamond_lines) as $key) { // Runs 'Diamonds' array
$opened_File[$key] = substr($opened_File[$key], 0, -1) . $string_Data; // Removes the last character from 'Diamond' line (new line chracter) and adds $string_Data variable at the end
}
//var_dump($opened_File);
$f = fopen($file_To_Edit, 'w');
fwrite($f, implode("\n", $opened_File)); // Writes new .CSV file
fclose($f);
?>
<?php
$file = "item_Data.csv"
$tmpFile = $file .".tmp";
$in = fopen($file, "r")
$out = fopen($tmpFile, "w")
while (($buffer = fgets($in)) !== false) {
if (preg_match('/my search pattern/', $buffer )) {
$buffer .= 'append this to the matched line';
}
fwrite($out, $buffer);
}
fclose($in);
fclose($out);
unlink($file);
rename($tmpFile, $file);
?>