Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php str_仅替换匹配的单词_Php_Replace_Str Replace - Fatal编程技术网

Php str_仅替换匹配的单词

Php str_仅替换匹配的单词,php,replace,str-replace,Php,Replace,Str Replace,我有这个PHP函数来用一些单词替换列表文件中文本中的单词 我的职能 function replace_text_wps($text){ $dir = plugin_dir_path( __FILE__ ); $file= $dir."bad2.list"; $badlist = file($file, FILE_IGNORE_NEW_LINES); $replace = '[censored]'; $text = str_replace($badlist, $replace,

我有这个PHP函数来用一些单词替换列表文件中文本中的单词

我的职能

function replace_text_wps($text){

$dir = plugin_dir_path( __FILE__ );
   $file= $dir."bad2.list";

$badlist = file($file, FILE_IGNORE_NEW_LINES);
$replace = '[censored]';

    $text = str_replace($badlist, $replace, $text);
    return $text;
}
例如,我的bad2.list中有ABC这个词

当我输入文本ABC时,我的函数将ABC更改为[censtered],但如果我在中输入单词DFGABC,则将其更改为DFG[censtered]

如何仅替换文件中的匹配词? 我是PHP新手?对不起,没有问题

更新:

,谢谢!你为我工作

这是工作版本

function replace_text_wps($text){

$dir = plugin_dir_path( __FILE__ );
   $file= $dir."bad2.list";

$badlist = file($file, FILE_IGNORE_NEW_LINES);

$replacement = "[CENSORED]";
$badlist = array_map(function($v) { return "\b". $v ."\b"; }, $badlist);
foreach($badlist as $f) {
    $text = preg_replace("/".$f."/u", $replacement, $text);


    return $text;
}
function replace_text_wps($text){

$dir = plugin_dir_path( __FILE__ );
   $file= $dir."bad2.list";

$badlist = file($file, FILE_IGNORE_NEW_LINES);

$replacement = "[CENSORED]";
$badlist = array_map(function($v) { return "\b". $v ."\b"; }, $badlist);
foreach($badlist as $f) {
    $text = preg_replace("/".$f."/u", $replacement, $text);


    return $text;
}

您可以使用一个数组,因此如果您是bad2.list文件,那么它的每一行都有所有的“坏”字,因此每行一个字,您可以这样做:

$file = file_get_contents("bad2.list"); //Should be a .txt....
$words = explode("\n", $file); //Explodes into a Array on each new line.

$message = "DFGABC";

foreach($words AS $word){
    $message = str_replace($word, "[censored]", $message);
}

echo $message;
一种可能的修复方法是在要审查的单词后添加一个空格,或者可以通过添加
$word=$word.'”来自动执行此操作
str_replace()之前


以下内容将按照您的要求执行。

您可以使用
preg\u replace()

$replace = '[censored]';

    $text = preg_replace("/\b$text\b/", $replace, $badlist);
    return $text;

这里有几个相互竞争的问题,其中一些是由我们提出的

这是一个定义单词的例子,您可能会认为
“是的,这是一个单词”
包含5个单词,但如果您使用空格系统来区分单词,例如

$badwords = array(" yes ", " this "); 
$text = "yes, this is a word"; 
print str_replace($badwords, "[censored]", $text);
输出将是
“是,[审查]是一个单词”

因为空格不定义字的形状;单词可以用任何形式包装,从新行字符到句号、各种标点符号,甚至没有空格,请尝试上述相同的系统,但:

$text = "this";
它不会替换冒犯性的单词,因为单词没有整齐地用空格包装

还存在一些问题,例如是否将连字符定义为分词?
“是的,先生”
是一个你想从中替换“是”的词吗?或者仅当“是”是单字实体时。。。这让我想起了当我看到一个在线交友网站删除了“鸡尾酒”这个词,因为它包含了一个粗鲁的词

所以我们怎样才能做到这一点? 正则表达式匹配,使用PHP函数和。我不认为有必要在这里重复这个问题,但这篇文章更多的是概述了试图使用简单的字符串替换函数进行regex智能查找和替换的众多陷阱


还请注意,您当前的函数不区分大小写,因此您不会匹配大小写或大写版本的脏话

如果你决定简单地在搜索中添加空白,你必须记住,你还需要添加相同的空白来保留替换文本的格式

更新:

,谢谢!你为我工作

这是工作版本

function replace_text_wps($text){

$dir = plugin_dir_path( __FILE__ );
   $file= $dir."bad2.list";

$badlist = file($file, FILE_IGNORE_NEW_LINES);

$replacement = "[CENSORED]";
$badlist = array_map(function($v) { return "\b". $v ."\b"; }, $badlist);
foreach($badlist as $f) {
    $text = preg_replace("/".$f."/u", $replacement, $text);


    return $text;
}
function replace_text_wps($text){

$dir = plugin_dir_path( __FILE__ );
   $file= $dir."bad2.list";

$badlist = file($file, FILE_IGNORE_NEW_LINES);

$replacement = "[CENSORED]";
$badlist = array_map(function($v) { return "\b". $v ."\b"; }, $badlist);
foreach($badlist as $f) {
    $text = preg_replace("/".$f."/u", $replacement, $text);


    return $text;
}

开关
$badlist
$text
可能重复。我建议您探索全能和雷霆万钧,以了解这些东西。使用带有
preg\u replace()
hd的单词边界,谢谢!你的解决方案对我有用!问题是,这仍然会替换word
DEFABC
中的
ABC
,而且不应该这样做。一个可能的解决方法是在ABC之后放置一个空格。因此请在回答中提及这一点。