I'm getting troubles on a simple php function! What i want to do is:
$text = "this is my data content with many words on it";
I want to write a function that turns the variable string $text as an array like this:
$array = array("this is my", "data content with", "many words on", "it");
In other words, each array piece should have 3 words on it!
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"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With