php分解:使用空格分隔符将字符串拆分为单词
工作正常,但空格仍会进入数组:php分解:使用空格分隔符将字符串拆分为单词,php,arrays,regex,preg-match,explode,Php,Arrays,Regex,Preg Match,Explode,工作正常,但空格仍会进入数组: $str = "This is a string"; $words = explode(" ", $str); 我更喜欢只使用没有空格的单词,并将有关空格数的信息分开 $words === array ('This', 'is', 'a', '', '', '', 'string');//true 刚刚添加:(1,1,4)表示第一个单词后有一个空格,第二个单词后有一个空格,第三个单词后有四个空格 有什么办法可以快速完成吗 谢谢。要将字符串拆分为数组,应使
$str = "This is a string";
$words = explode(" ", $str);
我更喜欢只使用没有空格的单词,并将有关空格数的信息分开
$words === array ('This', 'is', 'a', '', '', '', 'string');//true
刚刚添加:(1,1,4)
表示第一个单词后有一个空格,第二个单词后有一个空格,第三个单词后有四个空格
有什么办法可以快速完成吗
谢谢。要将字符串拆分为数组,应使用: 您的第二部分(计算空格):
您可以对第一个数组使用
preg\u split()
:
$string = 'This is a string';
preg_match_all('/\s+/', $string, $matches);
$result = array_map('strlen', $matches[0]);// [1, 1, 4]
和preg\u match\u all()
用于$spaces
数组:
$str = 'This is a string';
$words = preg_split('#\s+#', $str);
另一种方法是使用foreach循环
preg_match_all('#\s+#', $str, $m);
$spaces = array_map('strlen', $m[0]);
下面是一种方法,拆分字符串并运行一次正则表达式,然后解析结果以查看哪些片段被捕获为拆分(因此只有空格),或者哪些片段是单词:
$str = "This is a string";
$words = explode(" ", $str);
$spaces=array();
$others=array();
foreach($words as $word)
{
if($word==' ')
{
array_push($spaces,$word);
}
else
{
array_push($others,$word);
}
}
您可以看到$words
是:
$temp = preg_split('/(\s+)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$spaces = array();
$words = array_reduce( $temp, function( &$result, $item) use ( &$spaces) {
if( strlen( trim( $item)) === 0) {
$spaces[] = strlen( $item);
} else {
$result[] = $item;
}
return $result;
}, array());
Array
(
[0] => This
[1] => is
[2] => a
[3] => string
)
而$spaces
是:
$temp = preg_split('/(\s+)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$spaces = array();
$words = array_reduce( $temp, function( &$result, $item) use ( &$spaces) {
if( strlen( trim( $item)) === 0) {
$spaces[] = strlen( $item);
} else {
$result[] = $item;
}
return $result;
}, array());
Array
(
[0] => This
[1] => is
[2] => a
[3] => string
)
以下是性能测试的结果:
Array
(
[0] => 1
[1] => 1
[2] => 4
)
$str=“这是一个字符串”;
变量转储(time());
对于($i=1;$i为
[2] =>a
[3] =>字符串
)
排列
(
[0] => 1
[1] => 1
[2] => 4
)
内部(1378392871)
排列
(
[0]=>此
[1] =>是
[2] =>a
[3] =>字符串
)
排列
(
[0] => 1
[1] => 1
[2] => 4
)
int(1378392873)$financialYear=2015-2016代码>
$str = "This is a string";
var_dump(time());
for ($i=1;$i<100000;$i++){
//Alma Do Mundo - the winner
$rgData = preg_split('/\s+/', $str);
preg_match_all('/\s+/', $str, $rgMatches);
$rgResult = array_map('strlen', $rgMatches[0]);// [1,1,4]
}
print_r($rgData); print_r( $rgResult);
var_dump(time());
for ($i=1;$i<100000;$i++){
//nickb
$temp = preg_split('/(\s+)/', $str, -1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
$spaces = array();
$words = array_reduce( $temp, function( &$result, $item) use ( &$spaces) {
if( strlen( trim( $item)) === 0) {
$spaces[] = strlen( $item);
} else {
$result[] = $item;
}
return $result;
}, array());
}
print_r( $words); print_r( $spaces);
var_dump(time());
关于提问者的空格数在哪里?谢谢你的回答。但是,您丢失了关于中间空格数量的信息。这就是我在问题中提出的问题(请看黑体字)。@Haradzieniec刚刚输入,是的。谢谢你,我已经更新了。1,1,4表示第一个单词后有一个空格,第二个单词后有一个空格,第三个单词后有四个空格。你想要空格的数量还是每个空格的位置?@JasonMcCreary他想要每个空格组中连续空格的数量:“(1),”(1),“(4)
。谢谢。空格的数目。@Haradzieniec,我想你不明白其中的区别。空间数=6。这不是你想要的,谢谢。但是,它收集空格,但不包含单词之间空格数量的信息。非常感谢您的回答。我已经测试了您和Alma Do Mundo/silkfire的解决方案。所有的解决方案都很好,但是阿尔玛做芒多的工作要快两倍。无论如何,谢谢你的解决方案。如果你想的话,你可以比较两个问题(请马上看我对我自己问题的回答)。这和问题不一样!而且已经晚了很多年。
$test = explode('-',$financialYear);
echo $test[0]; // 2015
echo $test[1]; // 2016