preg_match expression is not working...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
elitecodex
Forum Newbie
Posts: 5
Joined: Thu Mar 11, 2004 2:01 pm
Location: FL

preg_match expression is not working...

Post 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!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

What exactly doesn't work? String "1234 5678" doesn't match your regexp?
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Re: preg_match expression is not working...

Post 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.
elitecodex
Forum Newbie
Posts: 5
Joined: Thu Mar 11, 2004 2:01 pm
Location: FL

Post 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.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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
Last edited by patrikG on Thu Mar 11, 2004 2:24 pm, edited 1 time in total.
elitecodex
Forum Newbie
Posts: 5
Joined: Thu Mar 11, 2004 2:01 pm
Location: FL

Post 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.
elitecodex
Forum Newbie
Posts: 5
Joined: Thu Mar 11, 2004 2:01 pm
Location: FL

Post by elitecodex »

patrikG - it worked
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

:)
elitecodex
Forum Newbie
Posts: 5
Joined: Thu Mar 11, 2004 2:01 pm
Location: FL

Post 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
Post Reply