thefollower Posted December 3, 2007 Share Posted December 3, 2007 How do you stop a string input being too long by cutting off the remaining chars from the string once it exceeds a set number of chars ? So like say max length was 50 characters.. and some one typed 52.. it will cut off the 2 characters at the end ? Quote Link to comment Share on other sites More sharing options...
Wuhtzu Posted December 3, 2007 Share Posted December 3, 2007 http://dk.php.net/substr <?php $string = "too long string"; $length = 8; $new_string = substr($string, 0, $length); ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted December 3, 2007 Share Posted December 3, 2007 Depends, if your DB is set to a length, yes, it will truncate. So you want to verify length via PHP before writing it to the DB. Quote Link to comment Share on other sites More sharing options...
ShoeLace1291 Posted December 3, 2007 Share Posted December 3, 2007 You could use substr with an if statement. Something like this would probably do the trick: if(strlen($_POST['input']) > 50){ $newstr = substr($_POST['input'], 0, -49 } 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.