papaface Posted April 21, 2007 Share Posted April 21, 2007 Hello, I know this has been answered A LOT of times but I can't remember how to do it. Can someone tell me how I would cut off a string: e.g. $string = "Hello everyone at PHP Freaks!" and cut it off at a certain amount of characters and add "..." at the end of the string when it is echo'ed. e.g Hello everyone at PH... click here read more Any help would be appreciated. regards Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/ Share on other sites More sharing options...
taith Posted April 21, 2007 Share Posted April 21, 2007 how right you are... lol... i post this one here all the time... we should make a database here of these functions lol <? function filter_charlimit($string, $length="50"){ if(strlen($string)<$length) return $string; else return trim(substr($string, 0, $length)).'...'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234742 Share on other sites More sharing options...
Glyde Posted April 21, 2007 Share Posted April 21, 2007 <?php $stringLength = 50; $string = "Hello everyone at PHP Freaks!"; $string = strlen($string) > $stringLength ? substr($string, 0, $stringLength) . "..." : $string; ?> Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234743 Share on other sites More sharing options...
papaface Posted April 21, 2007 Author Share Posted April 21, 2007 Thanks I will try that. Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234745 Share on other sites More sharing options...
papaface Posted April 21, 2007 Author Share Posted April 21, 2007 I have found a problem with this. When I have a html link and it just happens to end the string to put the ... in. It comes out: href="http://www.bu...%3Ca%20href=" morenews.php?morenews="1"">More Info</a> How can I stop it from adding the .. more info link when it is in the middle of a html tag? Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234911 Share on other sites More sharing options...
boo_lolly Posted April 21, 2007 Share Posted April 21, 2007 you've got to post more code. i was not aware that this was being used to a link... what are you trying to do exactly? Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234914 Share on other sites More sharing options...
papaface Posted April 21, 2007 Author Share Posted April 21, 2007 It isnt always used for a link. Just sometimes there is a link in the place it gets cut off. The code I am using is: if(!function_exists(filter_charlimit)) { function filter_charlimit($string,$length,$addition) { if(strlen($string)<$length) return $string; else return trim(substr($string, 0, $length)).$addition; } } and in the page: <?php echo filter_charlimit($news_content,100,'...<a href="morenews.php?morenews='.$news_id.'">More Info</a>'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234915 Share on other sites More sharing options...
AndyB Posted April 21, 2007 Share Posted April 21, 2007 You could do this: $news_content = strip_tags($news_content); // quick clean-up <?php echo filter_charlimit($news_content,100,'...<a href="morenews.php?morenews='.$news_id.'">More Info</a>'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234927 Share on other sites More sharing options...
papaface Posted April 21, 2007 Author Share Posted April 21, 2007 I cant use that, because it gets rid of all the <*br /> tags etc Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234931 Share on other sites More sharing options...
boo_lolly Posted April 21, 2007 Share Posted April 21, 2007 is this what you're looking for? <?php function truncate($string, $length, $url){ if(strlen($string) > $length){ return $string = substr($string, 0, $length) . "...<a href=\"{$url}\">Click here for more</a>"; }else{ return $string .= "...<a href=\"{$url}\">Click here for more</a>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234940 Share on other sites More sharing options...
papaface Posted April 21, 2007 Author Share Posted April 21, 2007 No, you dont understand. The text I am cutting has a HTML link in. In some cases it cuts the HTML short so i end up with a broken link and my ...More info link added on to the end. So neither work. Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234941 Share on other sites More sharing options...
AndyB Posted April 21, 2007 Share Posted April 21, 2007 http://ca.php.net/manual/en/function.strip-tags.php That actually allows you to specify tags to leave 'as is'. Why would you have br tags in your database anyway? Normally, the database entries contain 'newlines' and you convert those when necessary to html breaks by using the nl2br() function. If you have data with html links in AND you want them to remain then you're going to have to accept a character limit that's specified in a different way. Perhaps, explode the database on </a> or use strpos() to find where those exist. Bottom line: fully define the problem before you begin looking for solutions - on your own or with help here. Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234943 Share on other sites More sharing options...
papaface Posted April 21, 2007 Author Share Posted April 21, 2007 br tags are in the db because I use nl2br() when I use a text area in a certain section of the site. Thanks for the info. I will try it. Also I've become to notice people have become hostile around here. There's no need for it. I'm not forcing you to help me. People also forget that members like me do actually give back to the community in the same way. I think I will look elsewhere for my help next time, and just constantly give to the community rather than taking. Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234945 Share on other sites More sharing options...
boo_lolly Posted April 21, 2007 Share Posted April 21, 2007 br tags are in the db because I use nl2br() when I use a text area in a certain section of the site. Thanks for the info. I will try it. Also I've become to notice people have become hostile around here. There's no need for it. I'm not forcing you to help me. People also forget that members like me do actually give back to the community in the same way. I think I will look elsewhere for my help next time, and just constantly give to the community rather than taking. don't be so bitter papaface. i'm working on a function for you right now. i would have had it done by now, but i got side-tracked by the ganja. i'll post a working func soon. Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234946 Share on other sites More sharing options...
papaface Posted April 21, 2007 Author Share Posted April 21, 2007 I wasn't referring to anyone in particular, its just something I have noticed lately at PHPFreaks. Quote Link to comment https://forums.phpfreaks.com/topic/48028-solved-cut-off-and-add-a/#findComment-234952 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.