Jump to content

If Strlen(stiring) >= 31 Then...


dzhax
 Share

Recommended Posts

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

<?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;}?>

Edited by dsonesuk
Link to comment
Share on other sites

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

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...