繁体   English   中英

只有没有正则表达式才能匹配整个单词

[英]match whole word only without regex

由于我不能使用preg_match(UTF8支持以某种方式被破坏,它在本地工作但在生产中断)我想找到另一种方法来匹配黑名单的单词。 问题是,我想搜索字符串只搜索完全匹配,而不是第一次出现字符串。

这是我用preg_match做的

preg_match('/\b(badword)\b/', strtolower($string));

示例字符串:

$string = "This is a string containing badwords and one badword";

我想只匹配“坏词”(最后)而不是“坏词”。

strpos('badword', $string) matches the first one

有任何想法吗?

假设您可以进行一些预处理,您可以使用白色空格替换所有标点符号并将所有内容放在小写中,然后执行以下任一操作:

  • 使用strpos的东西,像这样strpos(' badword ', $string)在一个while循环,以保持通过整个文档迭代;
  • 将字符串拆分为空格,并将每个单词与您拥有的错误单词列表进行比较。

所以,如果你在尝试第一个选项,它会像这样(未经测试的伪代码)

$documet = body of text to process . ' ' 
$document.replace('!@#$%^&*(),./...', ' ')
$document.toLowerCase()
$arr_badWords = [...]
foreach($word in badwords)
{
    $badwordIndex = strpos(' ' . $word . ' ', $document)
    while(!badWordIndex)
    {
        //
        $badwordIndex = strpos($word, $document)
    }
}

编辑:根据@jonhopkins的建议,在最后添加一个空白区域应该满足那些希望单词位于文档末尾并且没有标点符号的情况。

如果你想模仿正则表达式的\\b修饰符你可以尝试这样的事情:

$offset = 0;
$word = 'badword';
$matched = array();
while(($pos = strpos($string, $word, $offset)) !== false) {
    $leftBoundary = false;
    // If is the first char, it has a boundary on the right
    if ($pos === 0) {
       $leftBoundary = true;
    // Else, if it is on the middle of the string, we must check the previous char
    } elseif ($pos > 0 && in_array($string[$pos-1], array(' ', '-',...)) {
        $leftBoundary = true;
    }

    $rightBoundary = false;
    // If is the last char, it has a boundary on the right
    if ($pos === (strlen($string) - 1)) {
       $rightBoundary = true;
    // Else, if it is on the middle of the string, we must check the next char
    } elseif ($pos < (strlen($string) - 1) && in_array($string[$pos+1], array(' ', '-',...)) {
        $rightBoundary = true;
    }

    // If it has both boundaries, we add the index to the matched ones...
    if ($leftBoundary && $rightBoundary) {
        $matched[] = $pos;
    }

    $offset = $pos + strlen($word);
}

您可以使用strrpos()而不是strpos

strrpos - 查找字符串中最后一次出现的子字符串的位置

$string = "This is a string containing badwords and one badword";
var_dump(strrpos($string, 'badword'));

输出:

45

使用带有unicode属性的单词边界的简单方法:

preg_match('/(?:^|[^pL\pN_])(badword)(?:[^pL\pN_]|$)/u', $string);

事实上它要复杂得多,请看这里

暂无
暂无

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

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