dzhax Posted October 9, 2009 Share Posted October 9, 2009 Title pretty much explains it.I haveif(strlen($songtitle) >= 31) echo $songtitle; else echo $songtitle;I need it to doif strlen(sontitle) >= 31 echo first 30 characters of songtitle followed by ... else echo song title.anyone know what to use? Link to comment Share on other sites More sharing options...
Synook Posted October 9, 2009 Share Posted October 9, 2009 http://www.php.net/manual/en/function.substr.php Link to comment Share on other sites More sharing options...
dsonesuk Posted October 9, 2009 Share Posted October 9, 2009 <?php$thistext = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.";if(strlen($thistext)>=31){echo substr($thistext,0,30)."...";}else{echo $thistext;}?> Link to comment Share on other sites More sharing options...
jeffman Posted October 9, 2009 Share Posted October 9, 2009 Advice:When you get to the online manual, you'll notice a pretty complete list of string functions on the left side of the page. Stare at it. Every time you go there. When you have time, click on the links. You don't have to memorize every function, but your programming will go much faster if you have a general sense of what functions are available.Same with array funcs, file funcs, MySql funcs, etc. You'll just be happier, I think. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 9, 2009 Share Posted October 9, 2009 Here's a function that uses substr to do that: function str_maxwidth($str, $w){ return (strlen($str) <= $w ? $str : (substr($str, 0, $w - 3) . '...'));} e.g.:echo str_maxwidth($song, 30);That one actually goes 3 characters back, so if it's greater than 30 it will show the first 27 followed by ..., you can see that in the substr function. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.