Jump to content

Archived

This topic is now archived and is closed to further replies.

Maverickb7

limit text?

Recommended Posts

Hello, I'm trying to limit the description of articles on my front page and can't seem to figure out how to do something. If the string is longer then X then it should be cut off and '...' three dots like that should be put at the end. If the string is shorter nothing will be done and no dots will appears. I've got it to got the string if its longer but I have no clue how to make it not do anything and not add the three dots if the string is shorter. Can anyone help? my code is below.

[code]function limitWithTags ($text, $length) {
        $text = str_replace(array('<BR>', '<br>', '<BR/>', '<br/>', '<BR />', '<br />'), '', $text);
        $len = strlen($text);
        $count = 0;

        $intag = 0;
        $inendtag = 0;
        $limit = 0;

        while ($i < $len) {

               switch ($c = $text{$i}) {

               case '<':
                        $intag = 1;
                        $short .= $c;
                        if ($text[$i+1]=='/') $inendtag = 1;
                        break;
               case '>':
                        $intag = 0;
                        $short .= $c;
                        if ($text[$i-1]=='/') $inendtag = 1;
                        if ($limit && $inendtag) break 2;
                        $inendtag = 0;
                        break;
               default:
                       if (!$intag) {
                            if ($count++ < $length)
                                $short .= $c;
                            else
                                $limit = 1;
                       }
                       else
                           $short .= $c;
               }
               ++$i;
        }
        $short = trim($short, ' ,');  // remove spaces and commas from end
        return $short . '...';
}[/code]

Share this post


Link to post
Share on other sites
[code]<?php

$max_len = 400; // max number of characters.

$string = "Your string";

if (str_len($string) > $max_len)
{
$string = substr($string,0,$max_length) . "...";
}

echo $string;
?>
[/code]

Share this post


Link to post
Share on other sites
Use the [a href=\"http://www.php.net/substr\" target=\"_blank\"]substr()[/a] function.

[code]<?php

shorten($str, $max)
{
      if (strlen($str) < $max) return($str);
      return (substr($str,0,$max) . '...');
}
?>[/code]

Ken

Share this post


Link to post
Share on other sites
this should work
[code]
function limiter($text,$len) {
$ret = "";
$text = strip_tags($text)
if(strlen($text) <= $len){
   $ret = $text
} else {
   $ret = strpos($text,0,$len);
   $ret .= "..."
}
return $ret;
}
[/code]

Share this post


Link to post
Share on other sites
I used your code in place of mine and got the following error.

Parse error: parse error, unexpected T_IF in /home/public_html/functions.php on line 21

I renamed the function and don't know whats causing it.

Line 21 is

[code]if(strlen($text) <= $len){[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=367279:date=Apr 21 2006, 02:29 PM:name=Maverickb7)--][div class=\'quotetop\']QUOTE(Maverickb7 @ Apr 21 2006, 02:29 PM) [snapback]367279[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I used your code in place of mine and got the following error.

Parse error: parse error, unexpected T_IF in /home/public_html/functions.php on line 21

I renamed the function and don't know whats causing it.

Line 21 is

[code]if(strlen($text) <= $len){[/code]
[/quote]

sorry i have missed ';'
function limiter($text,$len) {
$ret = "";
$text = strip_tags($text)
if(strlen($text) <= $len){
$ret = $text;
} else {
$ret = strpos($text,0,$len);
$ret .= "...";
}
return $ret;
}

Share this post


Link to post
Share on other sites
got it to load the page without errors. But theres still a problem. if the string is short it leaves it alone... if its longer it only shows the three dots.. and doesn't show the string?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.