[英]PHP to capitalize all letters (including after a slash) except for certain words
[英]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.