Gath Posted August 24, 2007 Share Posted August 24, 2007 Hi. I'm kind of lost. Trying to prevent that a subject from a message board to be created with only spaces, but cant manage to make it work. if (!empty($_POST['topic_title'])) { $topic_title = ereg_replace ( "^[ \t]+|[ \t]+$", '', $topic_title ); if (strlen ($topic_title) < 3) { $topic_title = ''; } else mysql_query (INSERT ... ... ... ); }; The only thing that might be working wrong is the ereg_replace, except, i copy/pasted it from another script i use and it works fine there. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/66553-solved-preventing-spaces-on-a-subject-or-message/ Share on other sites More sharing options...
GingerRobot Posted August 24, 2007 Share Posted August 24, 2007 Try stripping out any unneeded white space with trim before you check the length: <?php $str = ' '; $str = trim($str); if(strlen($str) < 3){ //invalid title }else{ //title is fine } ?> Of course, you'll also be wanting to use something like mysql_real_escape_string to prevent any sql injection. Quote Link to comment https://forums.phpfreaks.com/topic/66553-solved-preventing-spaces-on-a-subject-or-message/#findComment-333402 Share on other sites More sharing options...
MadTechie Posted August 24, 2007 Share Posted August 24, 2007 replace the ereg_replace with $topic_title = ereg_replace('^[[:space:]]+|[[:space:]]+$', '', $topic_title); to remove the spaces the one you currently have is for tab or space, the one above is for whitespace (both) but GingerRobot is correct trim will work Quote Link to comment https://forums.phpfreaks.com/topic/66553-solved-preventing-spaces-on-a-subject-or-message/#findComment-333412 Share on other sites More sharing options...
GingerRobot Posted August 24, 2007 Share Posted August 24, 2007 Using regular expressions just to remove the whitespace on the end is a bit overkill anyway. Using the trim function would be more efficient. Quote Link to comment https://forums.phpfreaks.com/topic/66553-solved-preventing-spaces-on-a-subject-or-message/#findComment-333423 Share on other sites More sharing options...
MadTechie Posted August 24, 2007 Share Posted August 24, 2007 i 100% agree, the reason for the "replacement regex" was just incase, RegEx is designed for complex replacements + other, trim is designed to .. well trim the string lol Quote Link to comment https://forums.phpfreaks.com/topic/66553-solved-preventing-spaces-on-a-subject-or-message/#findComment-333436 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.