1

因此,在创建从数据库中提取页面信息的动态网站时,我遇到了一个很常见的问题。假设您在首页上有一个滑块,显示 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 没有任何回声。

如果有人有任何想法或做这种事情的更好方法,请让我知道!

4

3 回答 3

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; 
}  
于 2012-10-27T00:53:35.970 回答
0

如果我是你,我不会做递归函数,而是 做一个 简单的函数

  • 爆炸“”是的,这是一个空格

  • 在计算每个字符串的字符之后

    ..让我演示给你看

    $array = explode(" ",$string); //让我们说

    $max_length =50; $输出=""; foreach($array as $value){ if(strlen($output.$value) > $max_length) break; 否则 $output.=$value; }

    回声$输出;

你怎么看 ?

于 2012-10-27T00:58:24.313 回答
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 设置为最小或最大字符。它也短了很多,但我也在使用它时添加了错误检查。

  1. Bound 为 'right'时,它就在 Length 之前。
  2. Bound 为 'left'时,它会在 Length 之后中断。

像这样使用:

var_dump(ChunkWordsByLengthMax($String, 120));
var_dump(ChunkWordsByLengthMin($String, 120));

希望能帮助到你。

于 2012-10-27T01:28:32.103 回答