仅mb字符串中的php第一个单词

问题描述 投票:0回答:1

我使用了preg_match,但它返回的pdf是英文,这可能是原因。

但是我只想得到练马春日町Ⅳ

有没有办法检测到它的mb字符串。

<?php 
// Initialize a sentence to a variable 
$sentence = '練馬春日町Ⅳ 清掃レポート.pdf'; 

// Use preg_match() function to get the 
// first word of a string 
preg_match('/\b\w+\b/i', $sentence, $result);  

// Display result 
echo "The first word of string is: ".$result[0]; 

?>

FIDDLE

php preg-match preg-split mbstring
1个回答
0
投票

为了使代码正常工作,您只需要在正则表达式中添加u标志,使其与Unicode字符匹配:

preg_match('/^\w+/iu', $sentence, $result);  
echo "\nThe first word of string is: ".$result[0];

输出:

The first word of string is: 練馬春日町Ⅳ

[请注意,由于您需要第一个单词,因此您只需将正则表达式与^一起锚定即可,而不需要第二个\b,因为\w+将匹配尽可能多的单词字符,即直到到达第一个单词休息。

或者,您可以将mb_split与正则表达式mb_split结合使用,该正则表达式与任何unicode空格或不可见分隔符匹配:

\p{Z}

输出:

\p{Z}

$sentence = '練馬春日町Ⅳ 清掃レポート.pdf'; $first_word = mb_split('\p{Z}', $sentence); echo $first_word[0];

© www.soinside.com 2019 - 2024. All rights reserved.