【发布时间】:2018-08-02 14:45:57
【问题描述】:
我需要在一个句子中找到所有出现的字符串数组(原始 $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.');
这是当前结果:
你好,我认为你并不聪明,你实际上是——而且——
结果应该是:
你好,我觉得你不聪明,你其实是d---和s------
关于如何解决这个问题的任何想法?谢谢!
【问题讨论】:
-
你想要破折号的数量与原始字长相同 -1 还是只是一些静态的破折号?
-
@anubhava 是的,破折号的数量应该与原始 -1 相同
标签: php regex str-replace input-sanitization