php phpexplode:使用空格作为分隔符将字符串拆分为单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18638753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:57:04  来源:igfitidea点击:

php explode: split string into words by using space a delimiter

phparraysregexpreg-matchexplode

提问by Haradzieniec

$str = "This is a    string";
$words = explode(" ", $str);

Works fine, but spaces still go into array:

工作正常,但空格仍然进入数组:

$words === array ('This', 'is', 'a', '', '', '', 'string');//true

I would prefer to have words only with no spaces and keep the information about the number of spacesseparate.

我宁愿只有没有空格的单词,并将有关空格数的信息分开。

$words === array ('This', 'is', 'a', 'string');//true
$spaces === array(1,1,4);//true

Just added: (1, 1, 4)means one space after the first word, one space after the second word and 4 spaces after the third word.

刚加:(1, 1, 4)表示第一个词后一个空格,第二个词后一个空格,第三个词后4个空格。

Is there any way to do it fast?

有什么办法可以快速做到吗?

Thank you.

谢谢你。

回答by Alma Do

For splitting the String into an array, you should use preg_split:

要将字符串拆分为数组,您应该使用preg_split

$string = 'This is a    string';
$data   = preg_split('/\s+/', $string);

Your second part (counting spaces):

您的第二部分(计算空格):

$string = 'This is a    string';
preg_match_all('/\s+/', $string, $matches);
$result = array_map('strlen', $matches[0]);// [1, 1, 4]

回答by Raj

$financialYear = 2015-2016;

$financialYear = 2015-2016;

$test = explode('-',$financialYear);
echo $test[0]; // 2015
echo $test[1]; // 2016

回答by nickb

Here is one way, splitting the string and running a regex once, then parsing the results to see which segments were captured as the split (and therefore only whitespace), or which ones are 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());

You can see from this demothat $wordsis:

你可以从这个演示中看到$words

Array
(
    [0] => This
    [1] => is
    [2] => a
    [3] => string
)

And $spacesis:

并且$spaces是:

Array
(
    [0] => 1
    [1] => 1
    [2] => 4
)

回答by silkfire

You can use preg_split()for the first array:

您可以preg_split()用于第一个数组:

$str   = 'This is a    string';
$words = preg_split('#\s+#', $str);

And preg_match_all()for the $spacesarray:

preg_match_all()对于$spaces数组:

preg_match_all('#\s+#', $str, $m);
$spaces = array_map('strlen', $m[0]);

回答by Ahmar Ali

Another way to do it would be using foreach loop.

另一种方法是使用 foreach 循环。

$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);
}
}

回答by Haradzieniec

Here are the results of performance tests:

以下是性能测试的结果:

$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());

int(1378392870) Array ( [0] => This [1] => is [2] => a [3] => string ) Array ( [0] => 1 [1] => 1 [2] => 4 ) int(1378392871) Array ( [0] => This [1] => is [2] => a [3] => string ) Array ( [0] => 1 [1] => 1 [2] => 4 ) int(1378392873)

int(1378392870) Array ( [0] => This [1] => is [2] => a [3] => string ) Array ( [0] => 1 [1] => 1 [2] => 4 ) int(1378392871) 数组 ( [0] => 这个 [1] => 是 [2] => a [3] => 字符串) 数组 ( [0] => 1 [1] => 1 [2] => 4 ) int(1378392873)