Maverickb7 Posted April 21, 2006 Share Posted April 21, 2006 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] Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 21, 2006 Share Posted April 21, 2006 [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] Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 21, 2006 Share Posted April 21, 2006 Use the [a href=\"http://www.php.net/substr\" target=\"_blank\"]substr()[/a] function.[code]<?phpshorten($str, $max){ if (strlen($str) < $max) return($str); return (substr($str,0,$max) . '...');}?>[/code]Ken Quote Link to comment Share on other sites More sharing options...
arifsor Posted April 21, 2006 Share Posted April 21, 2006 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] Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted April 21, 2006 Author Share Posted April 21, 2006 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 21I renamed the function and don't know whats causing it.Line 21 is [code]if(strlen($text) <= $len){[/code] Quote Link to comment Share on other sites More sharing options...
arifsor Posted April 21, 2006 Share Posted April 21, 2006 [!--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 21I 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;} Quote Link to comment Share on other sites More sharing options...
Maverickb7 Posted April 21, 2006 Author Share Posted April 21, 2006 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? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.