Explode text, but returning each array as three words

I'm having trouble with a simple php function! I want to do this:

$text = "this is my data content with many words on it";

I want to write a function that turns a variable string $ text into an array like this:

$array = array("this is my", "data content with", "many words on", "it");

In other words, each array must have 3 words on it!

+3
source share
6 answers

Taken from http://php.net/manual/en/function.preg-split.php :

<?php 
/** 
* Split a string into groups of words with a line no longer than $max 
* characters. 
* 
* @param string $string 
* @param integer $max 
* @return array 
**/ 
function split_words($string, $max = 1) 
{ 
$words = preg_split('/\s/', $string); 
$lines = array(); 
$line = ''; 

foreach ($words as $k => $word) { 
    $length = strlen($line . ' ' . $word); 
    if ($length <= $max) { 
        $line .= ' ' . $word; 
    } else if ($length > $max) { 
        if (!empty($line)) $lines[] = trim($line); 
        $line = $word; 
    } else { 
        $lines[] = trim($line) . ' ' . $word; 
        $line = ''; 
    } 
} 
$lines[] = ($line = trim($line)) ? $line : $word; 

return $lines; 
} 
?>

There are many ways you can do this — this option is probably not the fastest. Are you using this piece of code or not?

+1
source

This should work:

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

returns:

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

. .

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"
}

/(\ \S *) {1,3}/ 1 2 , {3}.

+2

    <?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;
}
?>
+1

Other answers seem too verbose. Here's a bit more idiomatic PHP to do this, and as an added bonus, the number of words per piece is a parameter.

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));
0
source

There must be a way to do this without regular expressions. Try the following:

<?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 will be an array, each member of which will contain a string with words. The last term can be shorter than three words, depending on the number of words in the original line.

0
source

Source: https://habr.com/ru/post/1774675/


All Articles