Jump to content

limit text?


Maverickb7

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]
Link to comment
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
Link to comment
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;
}
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.