Php 按一定数量的字符剔除文本字段,同时确保不截断单词

Php 按一定数量的字符剔除文本字段,同时确保不截断单词,php,string,recursion,trim,Php,String,Recursion,Trim,因此,当创建从数据库中提取页面信息的动态网站时,我遇到了一个非常常见的问题。假设你在头版有一个滑块,上面显示了8篇最新的博客/故事文章。服务器会获取每个项目的标题、标题和文本字段,但我们不想将文章的全文返回到浏览器,这将是浪费。所以我猜你会做的是使用递归函数在一定数量的字符处切断文本字段,除非我似乎无法找出我做错了什么。代码如下: $string = strip_tags("<p>Jackson expects to practice on Wednesday for the fir

因此,当创建从数据库中提取页面信息的动态网站时,我遇到了一个非常常见的问题。假设你在头版有一个滑块,上面显示了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;
$string=strip\u标签("杰克逊预计将在周三进行训练,这是他在与芝加哥熊队的赛季首场比赛中受伤后的第一次。球队的医疗顾问说,他扭伤的脚踝很快就痊愈了,尽管他警告说要谨慎行事。

主教练安迪·里德很乐观,他能及时为杰克逊下周一的比赛做好准备在对阵底特律狮子会的比赛中,他说他不想在3周后的季后赛开始时冒失去他的风险。

”; $count=0; echo$string。“
”; 函数trim\u string($string、$max\u length、$search\u char){ 全球美元计数; $pos=stripos($string,$search\u char); $new_string=substr($string,0,$pos); $length=strlen($new_字符串); 如果($length>$max_length){ $count++; 修剪线绳($new_线绳,120,); }否则{ 返回$new_字符串; } } $trimmed_string=trim_string($string,120,,); echo$count.“
”。substr\u count($string,”)。“
.”$trimmed\u string;
正如您所看到的,我正在尝试调试.Count返回67,而原始的出现次数是86,所以我知道它正在工作,但是$string没有任何echo输出


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

这不一定需要递归

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; 
}  

这不一定需要是递归的

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; 
}  

如果我是你,我不会做递归函数,而是 简单功能 哪个可以这样做

  • 是的,这是一个空格

  • 计数后每个字符串的字符

    …让我给你看看

    $array=explode(“,$string); //比方说

    $max_长度=50; $output=“”; foreach($array作为$value){ 如果(strlen($output.$value)>$max_length)中断; else$输出=$值; }

    回声输出


你觉得怎么样?

如果我是你,我不会做递归函数,而是做递归函数 简单功能 哪个可以这样做

  • 是的,这是一个空格

  • 计数后每个字符串的字符

    …让我给你看看

    $array=explode(“,$string); //比方说

    $max_长度=50; $output=“”; foreach($array作为$value){ 如果(strlen($output.$value)>$max_length)中断; else$输出=$值; }

    回声输出


你觉得怎么样?

试试这一款尺寸:

/**
* 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');
}

希望有帮助。

试试这一款看看尺寸:

/**
* 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');
}

希望能有所帮助。

我只是让函数在它再次调用自己之前回显新字符串,并知道它确实在工作,但我不明白为什么它缩短后不返回这个字符串。我只是让函数在它再次调用自己之前回显新字符串,并知道它确实在工作,但我不明白理解为什么它在缩短字符串后不返回该字符串。我知道必须有一种更简单的方法,以及我的值不返回递归的原因。谢谢!但我想知道,PHP中内置的一些字符串函数是递归的吗?另外,我想在大规模设置中,当存储在数据库中并将较小的字符串保存在单独的字段中时,可能需要运行此函数一次。这种额外的数据库开销可能比在每次页面调用中运行类似的函数要高效得多,并且比必须将全文发送到客户端以运行类似的函数要高效得多n用户端。是的,在数据库中存储这样的东西是有意义的,尤其是在循环中。但是,如果你在更新时将动态页面写入平面文本,那么这并不重要。我将存储在数据库中,并亲自写入平面文本。我总是更喜欢在可能的情况下动态提供预呈现的html。我知道必须有一种更简单的方法,以及我的值没有返回递归的原因。谢谢!不过,我想知道,这些内置在PHP中的字符串函数中的一些函数本身是递归的吗?而且,我认为,在大规模设置中,当存储在DB和将这个较小的字符串保留在单独的字段中。这种额外的数据库开销可能比在每次页面调用中运行类似的函数要高效得多,并且比必须将全文发送到客户端以便在用户端运行类似的函数要高效得多。是的,在数据库中过度运行是有意义的类似这样的东西,尤其是在循环中。但是,如果您在更新时将动态页面写入平面文本,那么这并不重要。我会将其存储在db中,并亲自写入平面文本。我总是更喜欢动态地提供预呈现html