php regex code problem with + symbol

Discussion in 'PHP' started by tycoon79, Jan 23, 2009.

  1. #1
    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; 
    
    Code (markup):
    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
     
    tycoon79, Jan 23, 2009 IP
  2. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #2
    Include a literal '+' sign in your preg
    Notice that I have put a backslash before that '+' sign, its because I don't want to confuse PHP whether it is a pattern modifier.

    $pattern='/^[a-zA-Z- .,\+]{2,200}$/'
    PHP:
     
    rohan_shenoy, Jan 23, 2009 IP
  3. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #3
    That is called escaping.
     
    Kaizoku, Jan 23, 2009 IP
  4. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #4
    By any chance is $ValString coming from $_GET or somewhere else where a plus symbol is synonymous with a space, after being urldecoded ?

    I see your pattern allows for spaces.
     
    joebert, Jan 24, 2009 IP
  5. bird.23

    bird.23 Peon

    Messages:
    13
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    <?
    $Valstring = "hulu+";
    $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;
    }
    }
    print $ValResult;
    ?>
    this way i am getting result as 0.May be u r getting the value of $Valstring from somewhere else that can change the meaning of + .
     
    bird.23, Jan 24, 2009 IP
  6. tycoon79

    tycoon79 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanx guys i will try out all your suggestion
     
    tycoon79, Jan 25, 2009 IP