Regular expression that match letters from all language php

廉价感情. 提交于 2020-03-16 08:12:29

问题


im trying for few hours to find the right regular expression in php to match any language letters but to prevent it to allow space

i have try this

[^\p{L}]

this is ok but it look like it allow the space

then i have try this

[^\w_-]

and it still look that it allow space

anyone can help with this please ?


回答1:


You need to specify the Unicode modifier u to get Unicode character properties in PCRE.

For example...

$pattern = "/([\p{L}]+)/u";
$string  = "你好,世界!Привет мир! !مرحبا بالعالم";
if (preg_match_all($pattern, $string, $match)) {
    var_dump($match);
}

Gives us...

array(2) {
  [0]=>
  array(6) {
    [0]=>
    string(6) "你好"
    [1]=>
    string(6) "世界"
    [2]=>
    string(12) "Привет"
    [3]=>
    string(6) "мир"
    [4]=>
    string(10) "مرحبا"
    [5]=>
    string(14) "بالعالم"
  }
  [1]=>
  array(6) {
    [0]=>
    string(6) "你好"
    [1]=>
    string(6) "世界"
    [2]=>
    string(12) "Привет"
    [3]=>
    string(6) "мир"
    [4]=>
    string(10) "مرحبا"
    [5]=>
    string(14) "بالعالم"
  }
}


来源:https://stackoverflow.com/questions/38938882/regular-expression-that-match-letters-from-all-language-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!