suzzane2020 Posted May 30, 2007 Share Posted May 30, 2007 Hi , I have a prob here that is drivin me nuts.. I am using a FCK editor and i want to validate the textarea value. I want to check if the value contains only space characters and if so give an error. I have tried the trim function but that is not working. I wud really appreciate if someone cud help Quote Link to comment Share on other sites More sharing options...
obsidian Posted May 30, 2007 Share Posted May 30, 2007 If it only contains spaces, you can use empty() to check it: <?php $string = " "; if (empty($string)) { // $string is still empty } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 30, 2007 Share Posted May 30, 2007 what do u mean ? example cdsdsdfdsfdsfdsfsdsdfdfsfsff dfdsfdsfsdfdsfsdfds <<<< not valid vcxcxxcvcvxcvxcvxvcxvxvvxvxvxvvxcvxvxvxvxv <<<< valid is that what u wont. Quote Link to comment Share on other sites More sharing options...
suzzane2020 Posted May 30, 2007 Author Share Posted May 30, 2007 obsidian- its not working.still gives no error. But when i echo the string ,nothing appears red arrow- --invalid If the string contains nothin but spaces i want to show an error. Quote Link to comment Share on other sites More sharing options...
obsidian Posted May 30, 2007 Share Posted May 30, 2007 obsidian- its not working.still gives no error. But when i echo the string ,nothing appears Ah, so you're actually wanting to check against HTML spaces... my bad. I would say you have to translate them first specifically for your check. Try something like this: <?php // return false if only spaces, true otherwise function checkSpaces($string) { $new = str_replace(' ', ' ', $string); return empty($new) } $string = ' '; if (checkSpaces($string)) { // invalid string... contains only spaces } ?> Hope that helps. Quote Link to comment Share on other sites More sharing options...
chigley Posted May 30, 2007 Share Posted May 30, 2007 <?php $string = " "; $chars = preg_split('//', $string, -1, PREG_SPLIT_NO_EMPTY); foreach($chars as $char) { if($char != " ") die("The string can only contain spaces"); } ?> Longwinded, but it should work. Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 30, 2007 Share Posted May 30, 2007 could you not use something like: if(ereg('^[[:space:]]*$', $string){ echo 'error! cannot contain just spaces'; } Quote Link to comment Share on other sites More sharing options...
suzzane2020 Posted May 30, 2007 Author Share Posted May 30, 2007 sorry for the late response ,i was trying out all the options u gave me. but the in the option if(ereg('^[[:space:]]*$', $string){ echo 'error! cannot contain just spaces'; } the prob is that it checks for only one space character just not working Quote Link to comment Share on other sites More sharing options...
chigley Posted May 30, 2007 Share Posted May 30, 2007 Does my method work? Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 30, 2007 Share Posted May 30, 2007 if(ereg('^[[:space:]]*$', $string){ echo 'error! cannot contain just spaces'; } should check for one or more space. the * symbol means one or more.. try: if(ereg('^([[:space:]]*)$', $string){ echo 'error! cannot contain just spaces'; } Quote Link to comment Share on other sites More sharing options...
Barand Posted May 30, 2007 Share Posted May 30, 2007 or try <?php $str = ' '; if (str_replace(array(' ', ' '), '', $str) == '' ) echo "spaces only"; ?> Quote Link to comment Share on other sites More sharing options...
suzzane2020 Posted May 30, 2007 Author Share Posted May 30, 2007 ??? not working..its still not checkin for more than one spaces. chigley - no ur method does not work too. but thanx or trying to help Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 30, 2007 Share Posted May 30, 2007 I've just tried this exact code and it works fine for me.. although it doesn't check for the actual html ' ' it just checks for one or more whitespaces. <?php if(isset($_POST['submit'])){ //if post is submit then... if(ereg('^[[:space:]]*$', $_POST['string'])){ echo 'error! cannot contain just spaces'; }else{ echo 'correct!'; } }else{ ?> <form name="staffedit" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"> <input type="text" name="string" value="" /> <input type="submit" name="submit" value="submit" /> </form> <?php } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted May 30, 2007 Share Posted May 30, 2007 try this ok <?php $string=' '; if(eregi('^[[:space:]]*$', $string) || (eregi('&bnsp;*', $string))){ echo 'error! cannot contain just spaces'; }else{ echo 'correct!'; } ?> Quote Link to comment Share on other sites More sharing options...
obsidian Posted May 30, 2007 Share Posted May 30, 2007 <?php eregi(' *', $string); ?> That will match if any non breaking spaces are found. It would also match   or ;;;;. Suzanne, did you try the method that effigy and I have both suggested? Basically, you'll want to replace all your ' ' characters with "real" spaces, then compare your string. If it still contains only spaces, you know you've got a problem. Here's a commented summary: <?php // Define an array of all HTML characters you want to consider spaces $spaces = array(' '); // first, create a comparable string by changing all HTML characters in your list to real spaces $comp_string = str_replace($spaces, ' ', $string); // Now, simply make sure that the string isn't spaces from beginning to end: if (preg_match('|^\s*\z|', $comp_string)) { // Only contains spaces } ?> Hope that helps Quote Link to comment Share on other sites More sharing options...
whoeverunomeaz Posted July 31, 2009 Share Posted July 31, 2009 $string = ' abra cadabra ';// check characters with spaces $string = ' '; // check only whitespaces function TrimStr($str) { $str = trim($str); for($i=0;$i < strlen($str);$i++) { if(substr($str, $i, 1) != " ") { $ret_str .= trim(substr($str, $i, 1)); } else { while(substr($str,$i,1) == " ") { $i++; } $ret_str.= " "; $i--; // *** } } return $ret_str; } $newstring = TrimStr($string) ; echo "<BR>\$newstring is: " . $newstring; if($newstring <1){ echo "<BR>\$string had just spaces in it!"; } Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted July 31, 2009 Share Posted July 31, 2009 use trim(). <?php $string = " "; if (!trim($string)) { echo '$string is still empty'; } Quote Link to comment Share on other sites More sharing options...
whoeverunomeaz Posted July 31, 2009 Share Posted July 31, 2009 Sorry, I posted an error... since a string could numerically be zero, the code should be: $string = ' abra cadabra ';// check characters with spaces $string = ' 000 '; // check only whitespaces function TrimStr($str) { $str = trim($str); for($i=0;$i < strlen($str);$i++) { if(substr($str, $i, 1) != " ") { $ret_str .= trim(substr($str, $i, 1)); } else { while(substr($str,$i,1) == " ") { $i++; } $ret_str.= " "; $i--; // *** } } return $ret_str; } $newstring = TrimStr($string) ; echo "<BR>\$newstring is: " . $newstring; if($newstring == NULL){ echo "<BR>\$string had just spaces in it!"; } Quote Link to comment Share on other sites More sharing options...
TeNDoLLA Posted July 31, 2009 Share Posted July 31, 2009 Are you the original poster also or?... dunno but here is some variations. <?php // Remove html entity spaces and normal spaces. $string = " "; $string = trim(str_replace(' ', '', $string)); $msg = !strlen($string) ? 'Empty string.' : ''; echo $msg; // Empty string. // Remove leading and following spaces. $str = ' 000 '; echo trim($str); // "000" // Same as above. $str = ' abra cadabra '; echo trim($str); // abra cadabra // Replace all spaces in string. $str = ' abra cadabra '; echo str_replace(' ', '', $str); // abracadabra // Find if string had atleast one space in it. if (preg_match('/\s/', $str, $matches)) { echo 'String contains space(s).'; } Quote Link to comment Share on other sites More sharing options...
roopurt18 Posted July 31, 2009 Share Posted July 31, 2009 You need to examine the raw output (or input) from the control to determine exactly what you need to remove. There could be empty paragraph tags, br tags, or all sorts of other crap submitted by a rich edit control that you'll have to handle. 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.