是否可以检查数组中的单词是否包含非英语字符

R R*_*R R 0 php

我关注此链接删除非英语字符PHP

但我仍然想知道是否有可能检查数组中的单词是否包含非英语字符.如果是啊?

谢谢!

h2o*_*ooo 6

要几乎完全复制粘贴来自其他线程的答案,您可以使用preg_match:

$foundNonEnglishCharacter = false;

foreach ($words as $word) {
    if (preg_match('/[^\00-\255]/', $word)) {
        $foundNonEnglishCharacter = true;
        break;
    }
}

var_dump($foundNonEnglishCharacter); //If true, there's a non-english character somewhere - if not, then there's no english characters.
Run Code Online (Sandbox Code Playgroud)

正则表达式尸检:

[^\00-\255]- 任何不在 ASCII值0到255范围内的字符(因此,如果匹配,则包含此范围之外的字符)

你可以找到常规的0-255 ascii值,以及它们在asciitable.com上的含义

  • @RishabhRaj:使用`array_filter`:`$ new = array_filter($ array,function($ word){return preg_match('/ [^\00-\255] /',$ word);});` (2认同)