masgas Posted November 14, 2006 Share Posted November 14, 2006 I'd like to print the latest messages from a forum in the index... the question is...how do I limit the length of the topic's names so the table that holds it doesn't become too large and makes the whole design crazy!is there anyway?here is the echo I've got...<?php echo "<a href='sec_sind/view_topic.php?id=$id' target='_self'>".$last_topic."</a>"?>I want to limit $last_topic let's say to 10 chars max.thank you in advance! Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 14, 2006 Share Posted November 14, 2006 In cases like this, substr() is your friend. It's great to run a function that will format it as you want. For instance, if you want 10 characters to be the absolute limit, you could always trim it to 7 and add elipsis as your final 3 characters, too:[code]<?phpfunction shortenMe($String, $limit = 10) { // default to 10 characters, but you can allow for whatever you wish if (strlen($String) > $limit) { $String = substr($String, 0, ($limit - 3)) . "..."; } return $String;}$title = "Hello to all those of you who know how to shorten post title!";echo shortenMe($title); // Hello t...?>[/code]Hope this helps! Quote Link to comment Share on other sites More sharing options...
Jenk Posted November 14, 2006 Share Posted November 14, 2006 To prevent words being cut in half, you can use this simple step:[code]<?php$string = 'one two three four five six seven eight';$part = substr($string, 0, 20);echo substr($part, 0, strrpos($part, ' '));?>[/code] Quote Link to comment Share on other sites More sharing options...
masgas Posted November 14, 2006 Author Share Posted November 14, 2006 Excellent, I'll give it a try and let you know!!!Thanks to both!!! Quote Link to comment Share on other sites More sharing options...
obsidian Posted November 14, 2006 Share Posted November 14, 2006 [quote author=Jenk link=topic=114922.msg467754#msg467754 date=1163509995]To prevent words being cut in half, you can use this simple step:[code]<?php$string = 'one two three four five six seven eight';$part = substr($string, 0, 20);echo substr($part, 0, strrpos($part, ' '));?>[/code][/quote]Good call, Jenk. There are some more complex ways to get this basic functionality into a trim function with a little more control, but that's an incredibly simple way to get the basic function down. Thanks for the addition. Quote Link to comment Share on other sites More sharing options...
masgas Posted November 14, 2006 Author Share Posted November 14, 2006 It's working just fine!Thanks again. 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.