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 ? Link to comment https://forums.phpfreaks.com/topic/79993-solved-string-limit/ 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); ?> Link to comment https://forums.phpfreaks.com/topic/79993-solved-string-limit/#findComment-405270 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. Link to comment https://forums.phpfreaks.com/topic/79993-solved-string-limit/#findComment-405271 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 } Link to comment https://forums.phpfreaks.com/topic/79993-solved-string-limit/#findComment-405272 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.