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

php仅来自mb字符串的第一个单词,php,preg-match,preg-split,mbstring,Php,Preg Match,Preg Split,Mbstring,我使用了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

我使用了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]; 

?>


要使代码正常工作,只需将
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+
将匹配尽可能多的单词字符,即直到第一个分词

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

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

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