如何根据 php 中的名称替换 .txt 文件中的字符串?
How to replace a string in .txt files according to their name in php?
我有一个文件夹,其中包含多个名为 :
的 .txt 文件
A500_1.txt
A500_2.txt
A700_1.txt
A700_2.txt
A900_1.txt
...
在每个 .txt 文件中有:
PRXC1_|TB|CCAAO9-RC|9353970324463|24.99
PRXC1_|TB|CFEXK4-RC|9353970294766|84.99
PRXC1_|TB|CFEXK4-RC|9353970294773|84.99
...
我希望你:
如果文件名以 A500_ 开头,请将 "TB" 替换为 "MD"
如果文件名以 A700_ 开头,将 "TB" 替换为 "JB"
如果文件名以 A900_ 开头,请将 "TB" 替换为 "LD"
你是怎么做到的?
<?php
$oldMessage = 'TB';
$NewMessage = 'MD';
//read the entire string
$str=file_get_contents('./test/A500_1.txt');
//replace something in the file string
$str=str_replace($oldMessage, $NewMessage,$str);
//write the entire string
file_put_contents('./test/A500_1.txt', $str);
echo "good !";
?>
首先,将您的文件路径设置为变量 $path = './test/A500_1.txt';
,然后使用 basename 提取文件名。您现在将有一个变量来有条件地测试。然后考虑一下,因为您将多次执行此操作,所以它应该是一个易于重用的函数。这应该看起来像这样:
function processFile( $path ) {
$filename = basename( $path );
//read the entire string
$str = file_get_contents( $path );
//replace something in the file string
if ( strpos( $filename, 'A500_' ) === 0 ) {
$str = str_replace( 'TB', 'MD', $str );
} else if ( strpos( $filename, 'A700_' ) === 0 ) {
$str = str_replace( 'TB', 'JB', $str );
} else if ( strpos( $filename, 'A900_' ) === 0 ) {
$str = str_replace( 'TB', 'LD', $str );
} else {
// Return false if we don't know what to do with this file
return false;
}
//write the entire string
$writeResult = file_put_contents( $path, $str );
//return true after a file is written successfully, or false on failure
return $writeResult >= 0;
}
if(processFile( './test/A500_1.txt' )) echo "good!";
从这里开始,编写一个遍历所有要处理的文件的循环可能是个好主意。 scandir 可能是您要查找的内容。然后在这个循环中重新使用你的新函数。另请注意,我添加了一个检查以确保函数中的写入成功。最好不要总是假设文件可以写入并明确检查。
更新:自您提出以来,示例循环语法。
$files = scandir("./test/", 0);
foreach($files as $file)
{
if(processFile( $file )) echo "$file is good!";
}
我有一个文件夹,其中包含多个名为 :
的 .txt 文件A500_1.txt
A500_2.txt
A700_1.txt
A700_2.txt
A900_1.txt
...
在每个 .txt 文件中有:
PRXC1_|TB|CCAAO9-RC|9353970324463|24.99
PRXC1_|TB|CFEXK4-RC|9353970294766|84.99
PRXC1_|TB|CFEXK4-RC|9353970294773|84.99
...
我希望你:
如果文件名以 A500_ 开头,请将 "TB" 替换为 "MD"
如果文件名以 A700_ 开头,将 "TB" 替换为 "JB"
如果文件名以 A900_ 开头,请将 "TB" 替换为 "LD"
你是怎么做到的?
<?php
$oldMessage = 'TB';
$NewMessage = 'MD';
//read the entire string
$str=file_get_contents('./test/A500_1.txt');
//replace something in the file string
$str=str_replace($oldMessage, $NewMessage,$str);
//write the entire string
file_put_contents('./test/A500_1.txt', $str);
echo "good !";
?>
首先,将您的文件路径设置为变量 $path = './test/A500_1.txt';
,然后使用 basename 提取文件名。您现在将有一个变量来有条件地测试。然后考虑一下,因为您将多次执行此操作,所以它应该是一个易于重用的函数。这应该看起来像这样:
function processFile( $path ) {
$filename = basename( $path );
//read the entire string
$str = file_get_contents( $path );
//replace something in the file string
if ( strpos( $filename, 'A500_' ) === 0 ) {
$str = str_replace( 'TB', 'MD', $str );
} else if ( strpos( $filename, 'A700_' ) === 0 ) {
$str = str_replace( 'TB', 'JB', $str );
} else if ( strpos( $filename, 'A900_' ) === 0 ) {
$str = str_replace( 'TB', 'LD', $str );
} else {
// Return false if we don't know what to do with this file
return false;
}
//write the entire string
$writeResult = file_put_contents( $path, $str );
//return true after a file is written successfully, or false on failure
return $writeResult >= 0;
}
if(processFile( './test/A500_1.txt' )) echo "good!";
从这里开始,编写一个遍历所有要处理的文件的循环可能是个好主意。 scandir 可能是您要查找的内容。然后在这个循环中重新使用你的新函数。另请注意,我添加了一个检查以确保函数中的写入成功。最好不要总是假设文件可以写入并明确检查。
更新:自您提出以来,示例循环语法。
$files = scandir("./test/", 0);
foreach($files as $file)
{
if(processFile( $file )) echo "$file is good!";
}