【问题标题】:str_replace replace only match wordsstr_replace 只替换匹配的单词
【发布时间】:2016-09-15 22:45:38
【问题描述】:

我有这个 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 更改为 [censored] ,但如果我输入单词 DFGABC 将其更改为 DFG[censored]

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

更新:

HD,谢谢!你的solution 为我工作!

这是工作版

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;
}

【问题讨论】:

标签: php replace str-replace


【解决方案1】:

您可以改用preg_replace()

$replace = '[censored]';

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

【讨论】:

    【解决方案2】:

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

    这是一个定义什么是单词的例子,你可能认为"yes, this is a word"包含5个单词,但是如果你使用空格系统来区分单词比如

    $badwords = array(" yes ", " this "); 
    $text = "yes, this is a word"; 
    print str_replace($badwords, "[censored]", $text);
    

    输出将是"yes, [censored] is a word";

    因为空格不定义字形;单词可以用任何东西包裹,从换行符\n 到句号、各种标点符号甚至没有空格,请尝试上面的相同系统,但是:

    $text = "this";
    

    它不会替换有问题的单词,因为该单词没有整齐地包裹在每一侧的空白中。

    还有一些问题,例如您是否将连字符定义为分词? "yes-sir" 是您要替换“是”的词吗?还是仅当“是”是单字实体时? ...这让我想起了当我看到一个在线约会网站删除“鸡尾酒”这个词时,因为它包含一个粗鲁的词。

    那么....我们该怎么做呢?

    正则表达式匹配,使用 PHP 函数 preg_replacereading this stack overflow question and answers。我认为没有必要在这里重复该问题的内容,但这篇文章更多的是概述了尝试使用简单的字符串替换功能进行正则表达式智能查找和替换的众多陷阱。

    Regex Example


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

    如果您出于懒惰而决定在搜索中简单地添加空格,您必须记住,您还需要添加相同的空格以保留 替换 文本的格式。

    【讨论】:

      【解决方案3】:

      你可以使用一个数组,所以如果你是 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;
      

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

      以下将按照您的要求工作。

      【讨论】:

      • 问题是这仍然替换了单词DEFABC中的ABC,它不应该
      • 一个可能的解决方法是在 abc 之后放置一个空格。
      • 所以在你的回答中提到这一点。
      【解决方案4】:

      更新:

      HD,谢谢!你的solution 为我工作!

      这是工作版

      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;
      }
      

      【讨论】:

        猜你喜欢
        • 2012-03-29
        • 2012-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-25
        相关资源
        最近更新 更多