我遇到了一个简单的PHP函数的麻烦! 我想要做的是:爆炸文本,但返回每个数组作为三个字
$text = "this is my data content with many words on it";
我想写它可以将变量字符串$文本作为一个这样的数组的函数:
$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";
我想写它可以将变量字符串$文本作为一个这样的数组的函数:
$array = array("this is my", "data content with", "many words on", "it");
换句话说,每个阵列片应该有3个字就可以了!
从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;
}
?>
有很多方法加载后,你可以做到这一点 - 这个选项可能不是最快的。你是否使用这段代码很多?
怎么样
<?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;
}
?>
呃,名为$ tmp,$ res和$ tmpRes的变量... – 2010-11-13 14:25:30
令人难以置信地令人费解。 – linead 2010-11-13 15:16:46
其他的答案似乎过于冗长。这里有一些稍微更习惯的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));
这应该工作:
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个月的不活动之后,接受的答案转换为这一答案。 – lheurt 2012-03-22 13:55:36