Pattern Matching

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
munchmo
Forum Newbie
Posts: 1
Joined: Thu Oct 10, 2002 1:11 pm
Location: GA, USA
Contact:

Pattern Matching

Post 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.
rev
Forum Commoner
Posts: 52
Joined: Wed Oct 02, 2002 3:58 pm
Location: Atlanta, GA

Post 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
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

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

Code: Select all

$number = implode("-", $array);
BUT, if you want to find it out of a list of numbers... use preg_match_all
Post Reply