繁体   English   中英

PHP Regex在字符串中查找所有大写单词

[英]PHP Regex find all capitalize words in string

PHP Regex在字符串中查找所有大写单词:

$string = "test sample test: 2015. ŽYDRŪNAS PAVARDENIS";

preg_match_all('/\b([A-Z-][\p{L}\pL]+)\b/', $string, $matches);

var_dump($matches);

输出:

array(2) {
  [0]=>
  array(2) {
    [0]=>
    string(8) "YDRŪNAS"
    [1]=>
    string(10) "PAVARDENIS"
  }
  [1]=>
  array(2) {
    [0]=>
    string(8) "YDRŪNAS"
    [1]=>
    string(10) "PAVARDENIS"
  }
}

问题是消失符号' Ž '?

请问修改regex ,即不会删除UTF-8符号?

在线代码代码

在使用unicode字符串时,您基本上需要使用修饰符u选项。 但是,使用:upper:字符类也可以简化正则表达式,因为它将匹配所有大写的unicode字符。

像这样:

$string = "test sample test: 2015. ŽYDRŪNAS PAVARDENIS";

preg_match_all("/[[:upper:]]+/u", $string, $matches);
var_dump($matches);

输出:

array(1) {
  [0]=>
  array(2) {
    [0]=>
    string(10) "ŽYDRŪNAS"
    [1]=>
    string(10) "PAVARDENIS"
  }
}

演示

暂无
暂无

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

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