Warptweet Posted February 28, 2007 Share Posted February 28, 2007 I have this code that will retreive the names of Flash Movies/Games from my website... <?php include("include/connect.php"); $sql = "SELECT * FROM flashid ORDER BY flashid DESC LIMIT 20"; $res = mysql_query($sql) or die (mysql_error()); while($r=mysql_fetch_assoc($res)){ echo "<a href=\"viewflash.php?flashid=".$r['flashid']."\">".$r['flashname']."</a></br>"; } ?> Now, how would I make it so that if the name is longer than 20 letters, it will replace the last 3 letters with "..."? Because names longer than 20 letters will stretch my tables, so I suppose "..." will be good enough to simply signify the name is longer. Link to comment https://forums.phpfreaks.com/topic/40612-solved-display-a-maximum-20-letters/ Share on other sites More sharing options...
boo_lolly Posted February 28, 2007 Share Posted February 28, 2007 something like this: <?php if(strlen($string) >= 20){ $string = substr($string, 0, 17) ."..."; } ?> Link to comment https://forums.phpfreaks.com/topic/40612-solved-display-a-maximum-20-letters/#findComment-196417 Share on other sites More sharing options...
Warptweet Posted February 28, 2007 Author Share Posted February 28, 2007 Where would I put that in my code? Could you please be a bit more specific? Thanks for trying Link to comment https://forums.phpfreaks.com/topic/40612-solved-display-a-maximum-20-letters/#findComment-196433 Share on other sites More sharing options...
boo_lolly Posted February 28, 2007 Share Posted February 28, 2007 in my previous post, we're manipulating a string. this string is called $string. now, for you, it would be whatever string that you wanted to manipulate... so that would be $r['flashname']. so this is how you impliment it into your code: <?php include("include/connect.php"); $sql = "SELECT * FROM flashid ORDER BY flashid DESC LIMIT 20"; $res = mysql_query($sql) or die (mysql_error()); while($r=mysql_fetch_assoc($res)){ if(strlen($r['flashname']) >= 20){ $r['flashname'] = substr($r['flashname'], 0, 17) ."..."; } echo "<a href=\"viewflash.php?flashid=".$r['flashid']."\">".$r['flashname']."</a></br>"; } ?> P.S. don't forget to click the 'topic solved' mod at the bottom-left of this thread. Link to comment https://forums.phpfreaks.com/topic/40612-solved-display-a-maximum-20-letters/#findComment-196439 Share on other sites More sharing options...
brianbehrens Posted February 28, 2007 Share Posted February 28, 2007 a lot of helpful documentation on this can also be found at... http://us2.php.net/manual/en/function.strlen.php Link to comment https://forums.phpfreaks.com/topic/40612-solved-display-a-maximum-20-letters/#findComment-196445 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.