2个单词乘2个单词



我希望代码在php中获得2个单词乘2个单词示例:

stackoverflow the best site of asking

结果:

2 words : stackoverflow the
2 words : best site
2 words : of asking
$array_of_words = implode(' ',array_chunk(explode(' ',$text), 2));
print_r($array_of_words);

这应该会给你

0=>stackoverflow 1=>询问的最佳站点2=>

  • array_counk()

    将数组分块为大小较大的块。最后一个块可以包含小于大小的元素

  • 内爆()

    用胶串连接数组元素

@MyStream的答案很好,不过另一个选项是:

$str = 'stackoverflow the best site of asking';
preg_match_all('#S+(s+S+)?#', $str, $matches);
var_dump($matches[0]);

输出:

array(3) {
  [0]=>
  string(17) "stackoverflow the"
  [1]=>
  string(9) "best site"
  [2]=>
  string(9) "of asking"
}

最新更新