Jump to content


Photo

limit text?


  • Please log in to reply
6 replies to this topic

#1 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 21 April 2006 - 06:42 PM

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.

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


#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 21 April 2006 - 07:15 PM

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

follow me on twitter @PHPsycho

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 21 April 2006 - 07:16 PM

Use the [a href=\"http://www.php.net/substr\" target=\"_blank\"]substr()[/a] function.

<?php

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

Ken

#4 arifsor

arifsor
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationPakistan

Posted 21 April 2006 - 07:20 PM

this should work
function limiter($text,$len) {
 $ret = "";
 $text = strip_tags($text)
 if(strlen($text) <= $len){
   $ret = $text
 } else {
   $ret = strpos($text,0,$len);
   $ret .= "..."
 }
 return $ret;
}

---------------------------
Power Demand Respect
Arif Sorathia

#5 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 21 April 2006 - 07:29 PM

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

if(strlen($text) <= $len){


#6 arifsor

arifsor
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts
  • LocationPakistan

Posted 21 April 2006 - 07:35 PM

[!--quoteo(post=367279:date=Apr 21 2006, 02:29 PM:name=Maverickb7)--][div class=\'quotetop\']QUOTE(Maverickb7 @ Apr 21 2006, 02:29 PM) View Post[/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

if(strlen($text) <= $len){
[/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;
}

---------------------------
Power Demand Respect
Arif Sorathia

#7 Maverickb7

Maverickb7
  • Members
  • PipPipPip
  • Advanced Member
  • 112 posts

Posted 21 April 2006 - 07:54 PM

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users