Page 1 of 1
Pattern Matching
Posted: Thu Oct 10, 2002 1:11 pm
by munchmo
I'm trying to do some pattern maching using preg_match. I have the basic pattern down, but I can't figure out how to not have it match certain things. I am looking for something like the following ##-##-##-##-## where the # represents an actual number. However those numbers can be a single digit as well. That's not the problem. Mixed in with the data I'm searching, I can also have ##-##-##-##-##-##-## (that's six sets of numbers instead of five) and I dont' want that returned. I don't know how to tell it to only pull out the set of 5 numbers without it pulling the set of 6 as well.
Thanks.
Posted: Thu Oct 10, 2002 1:24 pm
by rev
If the dashes are your literal delimeter (included in your string per the example you listed) then I would simply count the dashes to conclude if you are receiving five or six sets of numbers.
Five sets (4 dashes): xx-x-xx-x-xx
Six sets (5 dashes): x-xx-xx-x-xx-x
As for matching the numbers, there are several ways of doing it. Since you did not elaborate too much on what you are doing it's sorta difficult to give you the best solution.
preg_match() may not be your best option, especially if you are simply wanting to pull the numbers out individually or something to the like... in this case you could simple explode the string on the dash, it seemingly being your delimeter.
http://www.php.net/manual/en/function.explode.php
If you must use preg_match(), then try the following documentation:
http://www.php.net/manual/en/function.preg-match.php
Posted: Thu Oct 10, 2002 2:06 pm
by hob_goblin
explode would be alot better:
Code: Select all
$number = "xx-xx-xx-xx-xx-xx"; // NOTE: HAS 6 NUMBERS
$array = explode("-", $number);
array_pop($array); // REMOVE LAST NUMBER
note, if you want to keep the "-" format you can then just do:
BUT, if you want to find it out of a list of numbers... use preg_match_all