Php 从excel字符串按空格分解
我尝试从excel中的字符串按空格分解Php 从excel字符串按空格分解,php,Php,我尝试从excel中的字符串按空格分解 <?php $string = 'RT 2'; print_r(explode(' ', $string)); // I try $parts = preg_split('/\s+/', $string); // and this preg_match("/([а-яА-Яa-zA-Z]*)([0-9]*)/iu", $string, $parts); 编辑:改为查看Karol Samborski的答案,添加u标志 将/s的范围更改为包括所有
<?php
$string = 'RT 2';
print_r(explode(' ', $string));
// I try
$parts = preg_split('/\s+/', $string);
// and this
preg_match("/([а-яА-Яa-zA-Z]*)([0-9]*)/iu", $string, $parts);
编辑:改为查看Karol Samborski的答案,添加u
标志
将/s
的范围更改为包括所有unicode空格
您可以使用非中断空格的Unicode序列,即\x00A0
(正则表达式格式),并将相应的/u
(Unicode)标志添加到正则表达式中:
$string = 'RT 2';
print_r(preg_split('/[\s\x00A0]+/u', $string));
演示:您的字符串中有一个特殊的空格,您可以始终使用dechex(ord())检查字节代码>或bin2hex()
或unpack()
此正则表达式包括普通空格字符、不间断空格和窄空格字符:
/[\x202F\x00A0\s]/
对正则表达式使用\u
,对preg匹配使用\x
(PCRE兼容)。我认为最简单的解决方案是:
<?php
$string = 'RT 2';
print_r(preg_split('/\s+/u', $string));
urlencode
是一种快速检查字符串中哪些字节值可能隐藏了“特殊”字符的方法。例如-@Teretto其中有一个不间断的空格。这与您试图拆分的普通空格字符不同。@Jeto好的,我如何删除所有空格和非中断空格以及其他用于分解字符串的内容?没错,您没有意识到/u
标志正在更改\s
本身的范围。向上投票。
/[\x202F\x00A0\s]/
<?php
$string = 'RT 2';
print_r(preg_split('/\s+/u', $string));