Jump to content

php regex code problem with + symbol


tycoon79

Recommended Posts

hi all..i need some help with php code

 

i have this code

$ValResult=0;
preg_match_all('/^[a-zA-Z- .,]{2,200}$/', $Valstring, $matches);
foreach($matches[0] as $value){
	if($value!='' || $value!='0' || $value!='null'){
		$ValResult=1;
	 }	
	 else{
		$ValResult=0;
	 }			
}
 return $ValResult; 

 

and try it with

 

1)$Valstring='hulu'; and its return $ValResult=1--ok

2)$Valstring='hulu-'; and its return $ValResult=1--ok

3)$Valstring='hulu123'; and its return $ValResult=0--ok

4)$Valstring='hulu]'; and its return $ValResult=0--ok

 

but the when i try it with

 

5)$Valstring='hulu+'; and its return $ValResult=1--not ok

 

it suppose to return $ValResult=0

 

can someone help me with..thanking you all in advance for reviewing my post

 

 

 

 

 

Link to comment
Share on other sites

I have altered the script so that it will make the + symbol as 0.

<?
$ValResult=0;
$Valstring='hulu+';
preg_match_all('/^[a-zA-Z- .,]{2,200}$/', $Valstring, $matches);
foreach($matches[0] as $value){
	if(!in_array($value,array('','0','null','+'))){
		$ValResult=1;
	 }	
	 else{
		$ValResult=0;
	 }			
}
 return $ValResult; 
?>

The way I altered the if function makes more sense.

Link to comment
Share on other sites

One thing immediately jumps out at me: /^[a-zA-Z- .,]{2,200}$/

When intending on using a dash character as a literal in a character class, always have it listed as either the very first (or very last) character, otherwise, this creates a range (not unlike 0-9).. so in that pattern, you are asking for a range from Z to space.. this creates unforseen problems.

 

Notice how CV's version puts the literal dash as the very first character? This is the correct way.

Link to comment
Share on other sites

As I mentioned before, since you are trying to match the full string, you don't need preg_match_all. Also, since you're simply trying to figure out if it matched, and return a 1 or 0 based off that, all of that code can be condensed down to this single line:

 

return preg_match('/^[-a-zA-Z .,]{2,200}$/', $Valstring);

 

if $Valstring matches the regex, true (1) will be returned.  If it doesn't, false (0) will be returned.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.