PHP: How do I detect if an input string is Arabic

2019-01-17 04:37发布

Is there a way to detect the language of the data being entered via the input field?

9条回答
狗以群分
2楼-- · 2019-01-17 05:04

this will check if the string is Arabic Or has Arabic text

text must be UNICODE e.g UTF-8

$str = "بسم الله";
if (preg_match('/[اأإء-ي]/ui', $str)) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
查看更多
劫难
3楼-- · 2019-01-17 05:04

I'm not aware of a PHP solution for this, no.

The Google Translate Ajax APIs may be for you, though.

Check out this Javascript snippet from the API docs: Example: Language Detection

查看更多
闹够了就滚
4楼-- · 2019-01-17 05:05

The PHP Text_LanguageDetect library is able to detect 52 languages. It's unit-tested and installable via composer and PEAR.

查看更多
登录 后发表回答