【问题标题】:PHP preg_split string with spaces and Turkish characters带有空格和土耳其字符的 PHP preg_split 字符串
【发布时间】:2019-08-07 06:47:32
【问题描述】:

我正在使用 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"
)

【问题讨论】:

    标签: php preg-split


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2016-10-09
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 2014-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多