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

3
我将使用 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"
)
1个回答

5
只需使用“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>';

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接