因此,在创建从数据库中提取页面信息的动态网站时,我遇到了一个非常常见的问题。假设首页上有一个滑块,显示最近的8篇博客/故事帖子。服务器抓取每个标题,标题和文本字段,但我们不希望将文章的全文返回给浏览器,这将是浪费。所以我猜你会做的是使用递归函数将文本字段切掉一定数量的字符,除了我似乎无法弄清楚我做错了什么。这是代码:
$string = strip_tags("<p>Jackson expects to practice on Wednesday for the first time since getting hurt in a season-opening game agains the Chicago Bears. The teams medical advisor says his sprained ankle has healed fast then expected, although he warns to err on the side of caution.</p><p>Coach Andy Reid is optimistic he can get Jackson ready in time for next Monday's square off vs the Detroit Lions, though he states that he doesn't want to take the risk of loosing him for the start of the playoffs in 3 weeks.</p>");
$count = 0;
echo $string."<br />";
function trim_string($string, $max_length, $search_char){
global $count;
$pos = strripos($string, $search_char);
$new_string = substr($string, 0, $pos);
$length = strlen($new_string);
if($length > $max_length){
$count++;
trim_string($new_string, 120, ' ');
}else{
return $new_string;
}
}
$trimmed_string = trim_string($string, 120, ' ');
echo $count."<br />".substr_count($string, ' ')."<br />".$trimmed_string;
正如您所看到的,我正在尝试调试。 Count返回67,而原始出现的次数是86,所以我知道它正在工作,但$ trimmed_string没有回显。
如果有人有任何想法或更好的方式做这种事情,那就知道了!
答案 0 :(得分:1)
这不一定需要递归。
function trim_string($string, $max_length, $search_char)
{
$string = substr($string, 0, $max_length);
$last_index = strrpos($string, $search_char);
return substr($string, 0, $last_index);
}
这是一个基本想法。如果你在某个不是空间或某物的地方砍伐,它可能会给你一个空白区域。
而且,它不会回应,因为你需要返回递归。
if($length > $max_length){
$count++;
return trim_string($new_string, 120, ' ');
}else{
return $new_string;
}
答案 1 :(得分:0)
如果我是你,我不会做递归功能但是 a 简单功能 这将做到这一点
在
后计算每个strgin的字符后..让我告诉你
$ array = explode(“”,$ string); //让我们说
$ max_length = 50; $输出= “”; foreach($ array as $ value){ if(strlen($ output。$ value)&gt; $ max_length)break; else $ output。= $ value; }
echo $ ouput;
答案 2 :(得分:0)
试一下这个的大小:
/**
* Keeps a (maximum or minimum) number of characters from a string.
* Will not break words in the process.
*
* If $RightBound === true the $Length is the maximum allowed.
* If $RightBound === false the $Length is the minimum allowed.
*
* @param string $String
* @param int $Length
* @param bool $RightBound
* @return string
*/
function ChunkWordsByLength($String, $Length, $Bound = true){
if(!is_string($String)){
trigger_error('$String must be a string.', E_USER_WARNING);
return false;
}
if(!is_numeric($Length) or (($Length = intval($Length)) < 1)){
trigger_error('$Length must be a positive integer.', E_USER_WARNING);
return false;
}
if(strlen($String) <= $Length){
return $String;
}
if(($Bound !== 'left') and ($Bound !== 'right')){
trigger_error('$Bound must be "left" or "right".', E_USER_WARNING);
}
if($Bound === 'right'){
$Pattern = "^.{0,{$Length}}\\b";
}else{
$Pattern = "^.{{$Length}}.*?\\b";
}
return preg_match("~{$Pattern}~", $String, $Slices) ? trim($Slices[0]) : false;
}
/**
* Alias of ChunkWordsByLength($String, $Length, 'right').
*
* @param string $String
* @param int $Length
* @return string
*/
function ChunkWordsByLengthMax($String, $Length){
return ChunkWordsByLength($String, $Length, 'right');
}
/**
* Alias of ChunkWordsByLength($String, $Length, 'left').
*
* @param string $String
* @param int $Length
* @return string
*/
function ChunkWordsByLengthMin($String, $Length){
return ChunkWordsByLength($String, $Length, 'left');
}
它使用RegExp ,允许您将Length设置为最小或最大字符。它也短得多,但我添加了错误检查,因为我也在使用它。
像这样使用:
var_dump(ChunkWordsByLengthMax($String, 120));
var_dump(ChunkWordsByLengthMin($String, 120));
希望它有所帮助。