我正在使用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>';
Run Code Online (Sandbox Code Playgroud)
此代码给出以下结果:
Array
(
[0] => textarea
[1] => name="custom_field"
[2] => label="Space space space"
[3] => column="1/2"
)
Run Code Online (Sandbox Code Playgroud)
这里一切都很好。
但是,如果我在土耳其语字符中添加空格,则无法按预期运行:
$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>';
Run Code Online (Sandbox Code Playgroud)
它用土耳其语字符分割字符串的中间:
Array
(
[0] => textarea
[1] => name="custom_field"
[2] => label="âç??
[3] => ?îö?üû"
[4] => column="1/2"
)
Run Code Online (Sandbox Code Playgroud)
如何检测preg_split中的土耳其语字符并将其保持在一个数组值中?像这样:
Array
(
[0] => textarea
[1] => name="custom_field"
[2] => label="âç?? ?îö?üû"
[3] => column="1/2"
)
Run Code Online (Sandbox Code Playgroud)
只需使用'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>';
Run Code Online (Sandbox Code Playgroud)