我在一个简单的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个单词!
答案 0 :(得分:2)
这应该有效:
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"
}
答案 1 :(得分:2)
只需一个正则表达式,您就可以轻松完成此操作。这里不需要循环。
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}。
答案 2 :(得分:1)
取自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;
}
?>
有很多方法可以做到 - 这个选项可能不是最快的。你是否经常使用这段代码?
答案 3 :(得分:1)
怎么样
<?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;
}
?>
答案 4 :(得分:0)
其他答案似乎过于冗长。这里有一些稍微更惯用的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));
答案 5 :(得分:0)
应该有一种没有正则表达式的方法。试试这个:
<?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将是一个数组,其中每个成员将包含一个包含单词的字符串。最后一个成员可能短于三个单词,具体取决于原始字符串中的单词数。