我preg_replace
用来在文本文件的一行中搜索单词匹配。如果找到,我想替换整行。我目前的问题是到目前为止我所尝试的只是替换了确切的单词而不是整行。
PHP
$database = "1.txt";
$id = "OFFENSIVEWORD1";
$str = file_get_contents($database);
$str = preg_replace("/\b".$id."\b/","********",$str);
$fp = fopen($database,'w');
fwrite($fp,$str);
fclose($fp);
1.TXT文件
LOVING YOU MY FRIEND etc.
OFFENSIVEWORD1 YOU MY FRIEND etc.
OFFENSIVEWORD2 YOU MY FRIEND etc.
OFFENSIVEWORD3 YOU MY FRIEND etc.
预期输出
LOVING YOU MY FRIEND etc.
********
OFFENSIVEWORD2 YOU MY FRIEND etc.
OFFENSIVEWORD3 YOU MY FRIEND etc.
谢谢。