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"
}
You can do this quite easily with just the one regex. There should be no need for a loop here.
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'));
Result :
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"
}
The basic regex is just /(\w\s*){1,3}/ if you dont want to catch the remaining 1 or 2 word runs you could just change the count to {3}.
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 loads of ways you can do it - this option is probably not the quickest. Are you using this piece of code a lot or not?
how about
<?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;
}
?>
The other answers seem overly verbose. Here's some slightly more idiomatic PHP to do it, and as an added bonus the number of words per chunk 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));
There should be a way of doing this without regular expressions. Try this:
<?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 the words. The last member may be shorter than three words depending on the number of words in the original string.
精彩评论