Page 1 of 1

preg_match expression is not working...

Posted: Thu Mar 11, 2004 2:01 pm
by elitecodex
Hello all!!

The input is "1234 5678" and has to allow for any symbol where the whitespace is (like a dash or something). Looks good to me, so if anyone could lend a hand, it would be great... thanks!!

Code: Select all

<?php

if ( strlen($_POST&#1111;'jobnumber'] == 9) && preg_match("/^(\d&#123;4&#125;&#1111;\-\s]\d&#123;4&#125;)$/", $_POST&#1111;'jobnumber']) )
		&#123;
			echo "It worked!!";		
		&#125;
?>
I have used this function many times and have always been able to figure it out... Im at a lost as to why this isnt working... makes sense to me. I also tried "/([0-9]{4})?([0-9]{4})/" and it didnt work either. Any information would be greatly appreciated... thanks!

Posted: Thu Mar 11, 2004 2:10 pm
by Weirdan
What exactly doesn't work? String "1234 5678" doesn't match your regexp?

Re: preg_match expression is not working...

Posted: Thu Mar 11, 2004 2:18 pm
by TheBentinel.com
elitecodex wrote:Hello all!!

The input is "1234 5678" and has to allow for any symbol where the whitespace is (like a dash or something). Looks good to me
Looks good to me, too. I downloaded this tool and checked your regexp: http://weitz.de/files/regex-coach.exe

It worked in that tool. It accepted "1234 5678" and "1234-5678", but not "1234_5678" or "123405678".

Are you sure your code is executing at all? Until you're comfortable with the regexp, I'd try the code as:

Code: Select all

print (preg_match("/^(\d{4}[\-\s]\d{4})$/", "1234 5678"))
It ought to output 0 if it doesn't match, 1 if it does.

Posted: Thu Mar 11, 2004 2:19 pm
by elitecodex
Right.

The strlen() part works just fine (I have dumped that out and seen it... returns true). The preg_match() returns false. Thats my problem.

Posted: Thu Mar 11, 2004 2:21 pm
by patrikG
Try

Code: Select all

$test="1234/5678";
if(preg_match("/^(\d{4}[\W]\d{4})$/", $test))
      {
         echo "It worked!!";      
      }
else
	echo "nope";
PHP Manual wrote: \W - any "non-word" character

Posted: Thu Mar 11, 2004 2:21 pm
by elitecodex
well, right before that line I do a var_dump of $_POST('jobnumber') and that gets executed, so I can see whats in it.

Ill have to download that tool when I get home... if it matches, why would it return not execute whats inside the if statement? This is strange...

I can paste the entire function if you like... But Im certain its being executed.

Posted: Thu Mar 11, 2004 2:24 pm
by elitecodex
patrikG - it worked

Posted: Thu Mar 11, 2004 2:24 pm
by patrikG
:)

Posted: Thu Mar 11, 2004 2:26 pm
by elitecodex
SON OF A #%$@&!

It was the stupid strlen() part... I dont understand... a var_dump($_POST['jobnumber']); says that its a string (9)... anyhow, I can figure that part out... I got the hard part out of the way

Thanks for all the help guys!!

Will