使用 PHP 从文本文件中查找并删除一个或多个(不区分大小写)单词
FInd and Remove one or more (with case insensitive) word from text file with PHP
需要代码方面的帮助才能获得上述主题的结果,
到目前为止,我已经使用了这个代码
<?php
$data = file_get_contents("pg1-afcCup.txt");
$data = explode(" ", $data);
$remv = "Hosts";
foreach($data as $val) {
if($remv != $val) {
$newdata .= $val . " ";
// print "$val<br />\n";
}
}
$newdata = substr($newdata, 0, -1); // remove trailing ,
print "<br /><br />String: $newdata";
$fp = fopen("pg2-afcCup.txt", "w+");
$fw = fwrite($fp, $newdata);
fclose($fp);
?>
以上代码会删除pg1-afcCup.txt中区分大小写的"Hosts"字,并重写保存在pg2-afcCup.txt.
中
如何让它变得不敏感,并找到不止一个词(用更多词填充$remv)?
ps:我试过preg_replace没有成功...
试试这个
<?php
$data = file_get_contents("pg1-afcCup.txt");
$data = explode(" ", $data);
$remv = "hosts";
$newdata = "";
foreach($data as $val) {
if($remv != strtolower($val)) {
$newdata .= $val . " ";
// print "$val<br />\n";
}
}
$newdata = substr($newdata, 0, -1); // remove trailing ,
print "<br /><br />String: $newdata";
$fp = fopen("pg2-afcCup.txt", "w+");
$fw = fwrite($fp, $newdata);
fclose($fp);
?>
它将字符串转换为小写然后检查。所以,这应该可以正常工作。
对于多个过滤器
<?php
$data = file_get_contents("pg1-afcCup.txt");
$data = explode(" ", $data);
$remv = array("hosts","other", "somemore");
$newdata="";
foreach($data as $val) {
if (! in_array(strtolower($val),$remv) ) {
$newdata .= $val . " ";
// print "$val<br />\n";
}
}
$newdata = substr($newdata, 0, -1); // remove trailing ,
print "<br /><br />String: $newdata";
$fp = fopen("pg2-afcCup.txt", "w+");
$fw = fwrite($fp, $newdata);
fclose($fp);
?>
你可以试试这个 preg_replace
<?php
$data = file_get_contents("pg.txt");
$data = explode(" ", $data);
$remv = "Hosts";
$pattern = '/^hosts$/i'; # if you need to erase the exact word
#$pattern = '/hosts/i'; # erasing a substring
foreach($data as $val) {
$newdata .= preg_replace($pattern, "", $val) . " ";
}
$newdata = substr($newdata, 0, -1); // remove trailing ,
print "<br /><br />String: $newdata";
$fp = fopen("pg2-afcCup.txt", "w+");
$fw = fwrite($fp, $newdata);
fclose($fp);
?>
需要代码方面的帮助才能获得上述主题的结果,
到目前为止,我已经使用了这个代码
<?php
$data = file_get_contents("pg1-afcCup.txt");
$data = explode(" ", $data);
$remv = "Hosts";
foreach($data as $val) {
if($remv != $val) {
$newdata .= $val . " ";
// print "$val<br />\n";
}
}
$newdata = substr($newdata, 0, -1); // remove trailing ,
print "<br /><br />String: $newdata";
$fp = fopen("pg2-afcCup.txt", "w+");
$fw = fwrite($fp, $newdata);
fclose($fp);
?>
以上代码会删除pg1-afcCup.txt中区分大小写的"Hosts"字,并重写保存在pg2-afcCup.txt.
中如何让它变得不敏感,并找到不止一个词(用更多词填充$remv)?
ps:我试过preg_replace没有成功...
试试这个
<?php
$data = file_get_contents("pg1-afcCup.txt");
$data = explode(" ", $data);
$remv = "hosts";
$newdata = "";
foreach($data as $val) {
if($remv != strtolower($val)) {
$newdata .= $val . " ";
// print "$val<br />\n";
}
}
$newdata = substr($newdata, 0, -1); // remove trailing ,
print "<br /><br />String: $newdata";
$fp = fopen("pg2-afcCup.txt", "w+");
$fw = fwrite($fp, $newdata);
fclose($fp);
?>
它将字符串转换为小写然后检查。所以,这应该可以正常工作。
对于多个过滤器
<?php
$data = file_get_contents("pg1-afcCup.txt");
$data = explode(" ", $data);
$remv = array("hosts","other", "somemore");
$newdata="";
foreach($data as $val) {
if (! in_array(strtolower($val),$remv) ) {
$newdata .= $val . " ";
// print "$val<br />\n";
}
}
$newdata = substr($newdata, 0, -1); // remove trailing ,
print "<br /><br />String: $newdata";
$fp = fopen("pg2-afcCup.txt", "w+");
$fw = fwrite($fp, $newdata);
fclose($fp);
?>
你可以试试这个 preg_replace
<?php
$data = file_get_contents("pg.txt");
$data = explode(" ", $data);
$remv = "Hosts";
$pattern = '/^hosts$/i'; # if you need to erase the exact word
#$pattern = '/hosts/i'; # erasing a substring
foreach($data as $val) {
$newdata .= preg_replace($pattern, "", $val) . " ";
}
$newdata = substr($newdata, 0, -1); // remove trailing ,
print "<br /><br />String: $newdata";
$fp = fopen("pg2-afcCup.txt", "w+");
$fw = fwrite($fp, $newdata);
fclose($fp);
?>