Forum Moderators: coopster

Message Too Old, No Replies

Regular help expression is confused!

is this ok, can it be improved

         

Matthew1980

6:14 pm on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there fellow WebmasterWorld'ers,

private function WhiteSpaceChecker((string)$input)

if(preg_match("/^(\s)+$/i", $input)){
return($this->Whitespace = true);
}
else{
return($this->Whitespace = false);
}
}


Simple enough function, can this be made any better at all?

Cheers,
MRb

enigma1

6:36 pm on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe you want to set the member Whitespace var and return it's value, if so a simplified version could be:

function WhiteSpaceChecker($input) {
$this->Whitespace = strpos($input, ' ') === false?false:true;
return $this->Whitespace;
}

rocknbil

9:53 pm on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This will check ONLY for one or more white spaces. The entire string begins and ends with white space. If that's your intent, OK.

if(preg_match("/^(\s)+$/i", $input)){

The ()'s are only needed if there are groupings in the match or in a preg_replace to save the value inside the () in a temporary variable. Not much difference between yours and this one, will work the same. There is no case sensitivity for white space. :-) May want the m and s though.

if(preg_match("/^\s+$/sm", $input)){

If you want the existence of white space in any string, the PHP functions will suffice, but for record's sake,

if(preg_match("/\s+/sm", $input)){

Matthew1980

8:43 pm on Sep 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Thanks for the advice. The idea is for me to check that the user hasnt just used the space bar and then pressed enter. My version works ok, but as i am not good with regex, so i thought i would consult the experts here.

Cheers,
MRb