且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 PHP 正则表达式匹配字符串中的任何 Unicode 空白字符

更新时间:2023-02-19 19:55:54

要匹配您可能使用的任何 1 个或多个 Unicode 空白字符

To match any 1 or more Unicode whitespace chars you may use

'~s+~u'

您的 '/[s ]/' 模式仅匹配单个空白字符 (s) 或制表符 ( )(这当然是多余的,因为 s 也已经匹配制表符了),但是由于缺少 u 修饰符,s 无法匹配 bw4 之后的 u00A0 字符(硬空格).

Your '/[s ]/' pattern only matches a single whitespace char (s) or a tab ( ) (which is of course redundant as s already matches tabs, too), but since the u modifier is missing, the s cannot match the u00A0 chars (hard spaces) you have after bw4.

所以,使用

$str = 'T bw4  05/09/19 07:51 am BW6N 499.803';
$strArr = preg_split('/s+/u', $str);
print_r($strArr);

查看 PHP 演示 产出

Array
(
    [0] => T
    [1] => bw4
    [2] => 05/09/19
    [3] => 07:51
    [4] => am
    [5] => BW6N
    [6] => 499.803
)