簡體   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