PHP preg_split 包含空格和土耳其语字符的字符串

PHP preg_split string with spaces and Turkish characters

我正在使用 preg_split 拆分以下字符串:

$string = 'textarea name="custom_field" label="Space space space" column="1/2"';
$preg_split = preg_split("/\s(?![\w\s]+\")/", $string);
echo '<pre>',print_r($preg_split,1),'</pre>';

此代码给出以下结果:

Array
(
    [0] => textarea
    [1] => name="custom_field"
    [2] => label="Space space space"
    [3] => column="1/2"
)

一切正常。

但是,如果我添加了带空格的土耳其语字符,它就无法正常工作:

$string = 'textarea name="custom_field" label="âçğı İîöşüû" column="1/2"';
$preg_split = preg_split("/\s(?![\w\s]+\")/", $string);
echo '<pre>',print_r($preg_split,1),'</pre>';

它将字符串的中间部分与土耳其语字符分开:

Array
(
    [0] => textarea
    [1] => name="custom_field"
    [2] => label="âçğı
    [3] => İîöşüû"
    [4] => column="1/2"
)

如何检测 preg_split 中的土耳其语字符并将它们保存在一个数组值中?像这样:

Array
(
    [0] => textarea
    [1] => name="custom_field"
    [2] => label="âçğı İîöşüû"
    [3] => column="1/2"
)

只需使用 'u' 修饰符(对于 utf8 字符串),如

$string = 'textarea name="custom_field" label="âçğı İîöşüû" column="1/2"';
$preg_split = preg_split("/\s(?![\w\s]+\")/u", $string);
echo '<pre>',print_r($preg_split,1),'</pre>';