我使用了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+
将匹配尽可能多的单词字符,即直到到达第一个单词休息。
或者,您可以将mb_split
与正则表达式mb_split
结合使用,该正则表达式与任何unicode空格或不可见分隔符匹配:
\p{Z}
输出:
\p{Z}
$sentence = '練馬春日町Ⅳ 清掃レポート.pdf';
$first_word = mb_split('\p{Z}', $sentence);
echo $first_word[0];