Php 分解文本,但将每个数组块返回为三个单词

Php 分解文本,但将每个数组块返回为三个单词,php,Php,我在一个简单的php函数上遇到了麻烦! 我想做的是: $text = "this is my data content with many words on it"; 我想编写一个函数,将变量字符串$text转换为如下数组: $array = array("this is my", "data content with", "many words on", "it"); 换句话说,每个数组块上应该有3个单词 摘自: 有很多方法可以做到这一点-这个选项可能不是最快的。您是否经常使用这段代码

我在一个简单的php函数上遇到了麻烦! 我想做的是:

$text = "this is my data content with many words on it";
我想编写一个函数,将变量字符串$text转换为如下数组:

$array = array("this is my", "data content with", "many words on", "it");
换句话说,每个数组块上应该有3个单词

摘自:


有很多方法可以做到这一点-这个选项可能不是最快的。您是否经常使用这段代码?

如何

    <?php
print_r(split3('this is my data content with many words on it'));

function split3($text){
    $tmp = explode(" ", $text);
    $res = array();
    for($i = 0; $i < count($tmp); $i+=3){
        $tmpRes = array();
        if(isset($tmp[$i])){ $tmpRes[] = $tmp[$i]; }
        if(isset($tmp[$i+1])){ $tmpRes[] = $tmp[$i+1]; }
        if(isset($tmp[$i+2])){ $tmpRes[] = $tmp[$i+2]; }
        $res[] = implode(" ", $tmpRes);
    }
    return $res;
}
?>

其他答案似乎过于冗长。这里有一些更为惯用的PHP来实现这一点,作为额外的好处,每个块的字数是一个参数

function create_word_chunks($text, $num_words) {
    $words = explode(' ', $text);

    $start = 0;
    $word_chunks = array();

    while ($start < count($words)) {
        $word_chunks[] = implode(' ', array_slice($words, $start, $num_words));
        $start += $num_words;
    }

    return $word_chunks;
}

$text = "this is my data content with many words on it";
var_dump(create_word_chunks($text, 3));
函数创建单词块($text,$num\u单词){
$words=分解(“”,$text);
$start=0;
$word_chunks=array();
while($start
这应该可以:

function split3($text)
{
    $array = array();
    foreach(explode(' ',$text) as $i=>$word)
    {
        if($i%3) {
            $array[floor($i/3)] .= ' '.$word;
        } else {
            $array[$i/3] = $word;
        }
    }
    return $array;
}

$text = "this is my data content with many words on it";
var_dump(split3($text));
返回:

array(4) {
  [0]=>
  string(10) "this is my"
  [1]=>
  string(17) "data content with"
  [2]=>
  string(13) "many words on"
  [3]=>
  string(2) "it"
}

只需使用一个正则表达式,就可以很容易地做到这一点。这里应该不需要循环

function splitWords($text, $noOfWords = 3) {
   $res = array();
   preg_match_all('/(\w+\s*){1,'.$noOfWords.'}/', $text, $res);

   return $res[0];
}

var_dump(splitWords('one one one two two two thre thre thre four four'));
结果:

array(4) {
  [0]=>
  string(12) "one one one "
  [1]=>
  string(12) "two two two "
  [2]=>
  string(15) "thre thre thre "
  [3]=>
  string(9) "four four"
}
基本正则表达式只是 /(\w\s*){1,3}/
如果您不想捕获剩余的1或2个单词运行,您可以将计数更改为{3}。

应该有一种不使用正则表达式的方法。试试这个:

<?php
//Second argument makes the function return an array of words
$words = str_word_count($text, 1);
foreach(array_chunk($words, 3) as $array){
    $pieces[] = implode(' ', $array);
}
?>


$pieces将是一个数组,其中的每个成员将包含一个包含单词的字符串。根据原始字符串中的字数,最后一个成员可能短于三个单词。

这并不能回答原始问题。应根据字数而不是行长进行拆分。我不知道为什么在15个月的不活动之后,人们接受的答案变成了这个。
<?php
//Second argument makes the function return an array of words
$words = str_word_count($text, 1);
foreach(array_chunk($words, 3) as $array){
    $pieces[] = implode(' ', $array);
}
?>