在句子中查找数组中所有字符串列表,并用短划线

时间:2018-02-22 16:38:32

标签: php regex str-replace input-sanitization

我需要在一个句子中找到所有出现的字符串数组(原始$ list有超过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.');

这是目前的结果:

  

你好,我认为你不聪明,你实际上是 - 而且 -

结果应为:

  

你好,我认为你不聪明,你实际上是你的 - 和 - -----

有关如何处理此问题的任何想法?谢谢!

3 个答案:

答案 0 :(得分:2)

您可以使用\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";

Fiddle

Code 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>

答案 1 :(得分:1)

您可以使用fetch生成仅包含第一个字母的替换数组,并为每个替换的字符生成一个短划线:

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.');

答案 2 :(得分:0)

您可以使用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