tags:

views:

20

answers:

1

Is it possible to make two matches of text - /123/123/123?edit

I need to match 123, 123 ,123 and edit

For the first(123,123,123): pattern is - ([^\/]+)
For the second(edit): pattern is - ([^\?=]*$)

Is it possible to match in one preg_match_all function, or I need to do it twice - one time for one pattern, second one for second?

Thanks !

+1  A: 

You can do this with a single preg_match_all call:

$string = '/123/123/123?edit';
$matches = array();
preg_match_all('#(?<=[/?])\w+#', $string, $matches);

/* $matches will be:
Array
(
    [0] => Array
        (
            [0] => 123
            [1] => 123
            [2] => 123
            [3] => edit
        )

)
*/

See this in action at http://www.ideone.com/eb2dy

The pattern ((?<=[/?])\w+) uses a lookbehind to assert that either a slash or a question mark must precede a sequence of word characters (\w is a shorthand class equivalent to [a-z0-9_]).

Daniel Vandersluis
Brilliant ! Thanks man!
Vovan