简体   繁体   English

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

[英]Find all occurrences of list of strings in array inside a sentence, and replace everything except the first letter with dashes

I need to find all occurrences of a array of strings (original $list has over 780 items) in a sentence, and replace everything except the first letter with html dashes. 我需要在一个句子中找到所有出现的字符串数组(原始$ list有超过780项),并用html破折号替换除第一个字母以外的所有内容。

This is my current code: 这是我目前的代码:

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

This is the current result: 这是目前的结果:

hello, i think you are not intelligent, you are actually – and – 你好,我认为你不聪明,你实际上 - 和 -

The result should be: 结果应该是:

hello, i think you are not intelligent, you are actually d––– and s––––– 你好,我认为你不聪明,你实际上是你的 - 和 - -----

Any ideas on how to approach this? 关于如何处理这个的任何想法? Thanks! 谢谢!

You may use this regex based approach using \\G : 你可以使用\\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 RegEx演示

Output: 输出:

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

  • \\G asserts position at the end of the previous match or the start of the string for the first match \\G在上一场比赛结束或第一场比赛的字符串开头处断言位置
  • (?!\\A) is negative lookahead to make sure \\G doesn't match at line start (?!\\A)是负向前瞻以确保\\G在行开始时不匹配

Update: 更新:

As per your comments below you can use this different approach: 根据您在下面的评论,您可以使用这种不同的方法:

$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);

Output: 输出:

first <span class="bad">w---</span>

You could use array_map to generate an array of replacements with the first letter only and optionally a dash for each character that was replaced: 您可以使用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.');

Result for your example is: hello, i think you are not intelligent, you are actually d--- and s-----. 你的例子的结果是: hello, i think you are not intelligent, you are actually d--- and s-----.

You can use preg_replace_callback but you need to add backslash to each item in $list array. 您可以使用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.');

Output: 输出:

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

Code demo 代码演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM