代码之家  ›  专栏  ›  技术社区  ›  Andres SK

在句子中查找数组中所有出现的字符串列表,并用破折号替换除第一个字母以外的所有内容

  •  1
  • Andres SK  · 技术社区  · 8 年前

    我需要在一个句子中找到所有出现的字符串数组(原始$列表有780多个项目),并用html破折号替换除第一个字母以外的所有内容。

    这是我当前的代码:

    function sanitize($string) {
        $list = array(
            "dumb",
            "stupid",
            "brainless"
        );
    
        # replace bad words
        $string = str_replace($list, '–', $string);
        return $string;
    }
    
    echo sanitize('hello, i think you are not intelligent, you are actually dumb and stupid.');
    

    这是当前结果:

    你好,我认为你不聪明,你实际上很聪明

    结果应该是:

    你好,我认为你不聪明,你实际上是d和s

    有什么办法吗?谢谢

    3 回复  |  直到 8 年前
        1
  •  2
  •   anubhava    8 年前

    您可以使用这种基于regex的方法 \G :

    $str = 'hello, i think you are not intelligent, you are actually dumb and stupid.';
    $list = array("dumb", "stupid", "brainless");
    
    // use array_map to generate a regex of array for each word
    $relist = array_map(function($s) { 
      return '/(?:\b(' . $s[0] . ')(?=' . substr($s, 1) . '\b)|(?!\A)\G)\pL/';
    }, $list);
    
    // call preg_replace using list of regex
    echo preg_replace($relist, '$1-', $str) . "\n";
    

    Code Demo

    RegEx Demo

    输出:

    hello, i think you are not intelligent, you are actually d--- and s-----.
    

    • \G级 在前一个匹配的结尾或第一个匹配的字符串的开头断言位置
    • (?!\A) 是负前瞻性的,以确保 \G级 行开始时不匹配

    更新:

    根据您在下面的评论,您可以使用这种不同的方法:

    $str = 'word';
    $relist = array_map(function($s) { return '/\b' . $s . '\b/'; }, $list);
    
    echo preg_replace_callback($relist, function($m) { 
       return '<span class="bad">' . $m[0][0] . str_repeat('-', strlen($m[0])-1) . '</span>';
    }, $str);
    

    输出:

    first <span class="bad">w---</span>
    
        2
  •  1
  •   cOle2    8 年前

    你可以使用 array_map 要生成仅包含第一个字母的替换数组,并且可以为替换的每个字符生成破折号,请执行以下操作:

    function sanitize($string) {
        $list = array(
            "dumb",
            "stupid",
            "brainless"
        );
    
        $repl = array_map("dashReplace", $list);
    
        # replace bad words
        $string = str_replace($list, $repl, $string);
        return $string;
    }
    
    function dashReplace($str) {
        return $str{0}.str_repeat("-", strlen($str)-1);
    }
    
    echo sanitize('hello, i think you are not intelligent, you are actually dumb and stupid.');
    

    示例的结果是: hello, i think you are not intelligent, you are actually d--- and s-----.

        3
  •  0
  •   Srdjan M.    8 年前

    您可以使用 preg_replace_callback 但您需要为中的每个项目添加反斜杠 $list 大堆

    function sanitize($string) {
        $list = array(
            "/dumb/",
            "/stupid/",
            "/brainless/"
        );
    
        # replace bad words
        $string = preg_replace_callback($list,
            function ($matches) {
                return preg_replace('/\B./', '-', $matches[0]);
            }, 
            $string);
        return $string;
    }
    
    echo sanitize('hello, i think you are not intelligent, you are actually dumb and stupid.');
    

    输出:

    hello, i think you are not intelligent, you are actually d--- and s-----.
    

    Code demo