Kaboom Posted January 5, 2010 Share Posted January 5, 2010 Okay so as you can see here: http://www.stephanddarryn.com/blog/2010/animals/index.php The description doesnt have a character limit, i need it to show only 300 characters bfore it puts... then cut the rest of the desctiption off so they have to view the whole thing to see the whole thing. How would I do this? I need character limit at 300 then if its over that put ... and cut the rest out and then that's it if its lower don't add the ... Quote Link to comment https://forums.phpfreaks.com/topic/187215-limit-characters-in-php/ Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 Okay so as you can see here: http://www.stephanddarryn.com/blog/2010/animals/index.php The description doesnt have a character limit, i need it to show only 300 characters bfore it puts... then cut the rest of the desctiption off so they have to view the whole thing to see the whole thing. How would I do this? I need character limit at 300 then if its over that put ... and cut the rest out and then that's it if its lower don't add the ... This should do the job more or less: $string = "Bla bla bla bla bla bla bla bla bla bla bla bla bla"; function shorten($string, $length) { if (strlen($string) > $length) { $string = substr($string, 0, $length)."..."; } return $string; } $shortdesc = shorten($string, 300); echo "$shortdesc"; //Outputs something 300 characters then ... EDIT: I'd recommend you look at strlen and substr they're handy string functions, if you know how to use them. Quote Link to comment https://forums.phpfreaks.com/topic/187215-limit-characters-in-php/#findComment-988644 Share on other sites More sharing options...
Kaboom Posted January 5, 2010 Author Share Posted January 5, 2010 It didnt work :'( Now it doesnt show them at all... hold on ill try something else Quote Link to comment https://forums.phpfreaks.com/topic/187215-limit-characters-in-php/#findComment-988649 Share on other sites More sharing options...
oni-kun Posted January 5, 2010 Share Posted January 5, 2010 It didnt work :'( Now it doesnt show them at all... hold on ill try something else How does it not work? If you look at a sample of the code: $string = 'abcdefghijklmnopqrstuvwxyxz'; //long string //[snip]// $shortdesc = shorten($string, 5); It'll output exactly: 'abcde...' How are you implementing it? Quote Link to comment https://forums.phpfreaks.com/topic/187215-limit-characters-in-php/#findComment-988653 Share on other sites More sharing options...
Kaboom Posted January 5, 2010 Author Share Posted January 5, 2010 The way it work is it connects $posts = mysql_query("SELECT * FROM databasenamehere ORDER BY id", $db_id); then prints utput as: $x = 1; while ( $blogpost = mysql_fetch_array( $posts ) ) { if ( $x != 1 ) { echo "<hr>"; } echo "<img src=\"http://www.elegantthemes.com/preview/eNews/wp-content/themes/eNews/timthumb.php?src=http://www.elegantthemes.com/images/sharedthumbs/8.jpg&h=74&w=73&zc=1\" alt=\"Animals\" width=\"73px\" height=\"74px\" /> <h2><a href=\"article.php?id=$blogpost[id]\">$blogpost[subject]</a></h2> <p class=\"info\"><em>author</em>: $blogpost[poster]</p> <p>$blogpost[content]</p>"; $x++; } $blogpost[content] is the one that displays the content of the post or description so i need to limit $blogpost[content] but when i set $string = blogpost[content]; then it makes it display nothing and wierd. it set to load content from every post rather then have certain defined content and array all the posts onto that page. I also need to make is so when it hits 50 posts a page it adds buttons for pages at bottom like: << < Page [1] 2 3 4 ... of 7 > >> thanks for what you've gave me but it's not working for this. When i managed to implement it it didnt show the post with under 300 at all Quote Link to comment https://forums.phpfreaks.com/topic/187215-limit-characters-in-php/#findComment-988656 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.