匹配来自所有语言php的字母的正则表达式 [英] Regular expression that match letters from all language php

查看:156
本文介绍了匹配来自所有语言php的字母的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图花几个小时在php中找到正确的正则表达式以匹配任何语言字母,但要防止它留出空间

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

我尝试过

[^\p{L}]

这没关系,但看起来可以留出空间

this is ok but it look like it allow the space

然后我尝试一下

[^\w_-]

并且看起来仍然允许空间

and it still look that it allow space

任何人都可以帮忙吗?

推荐答案

您需要指定Unicode修饰符u以获得 PCRE中的Unicode字符属性.

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

例如...

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

给我们...

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) "بالعالم"
  }
}

这篇关于匹配来自所有语言php的字母的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆