Sefa Yavuz
Sefa Yavuz

Reputation: 33

PHP - Preg match with multiple conditions

I have the following the following string:

"#(admin/pages|admin/pages/add|admin/pages/[0-9]+)#"

And this string to compare it to:

"admin/pages/1"

What I need is to return "admin/pages/1" when comparing the 2 strings using preg_match(). I have the following code and it's not working:

if(preg_match("#(admin/pages|admin/pages/add|admin/pages/[0-9]+)#", "admin/pages/1", $matches) {
   var_dump($matches); 
}

This is the output i get:

array(2) { [0]=> string(11) "admin/pages" [1]=> string(11) "admin/pages" }

Can anybody help out?

Upvotes: 1

Views: 3030

Answers (3)

RomanPerekhrest
RomanPerekhrest

Reputation: 92904

Use the following short regex parrent:

"#admin/pages(/add|/[0-9]+)?#"

(/add|/[0-9]+)? - optional alternative group, matches either /add or /<number> at the end of searched substring if occurs

Upvotes: 1

Imanuel
Imanuel

Reputation: 3667

Change your regex to:

"#(admin/pages(?:/\d+)?|admin/pages/add)#"

You don't need both variants (admin/pages|admin/pages/[0+9]+) if you put the digits in the first pattern and make them optional.

Question marks and repetitions are greedy by default, that's why it will always include the digits in the match for my version.
On the other hand, if you have an alternation, it will always pick the first match. Since your first alternation does not include digits, they are not matched.

If you're also wondering why you get your match two times, that's because of the way preg_match works.
Quote from the documentation:

$matches[0] will contain the text that matched the full pattern, $matches[1] will have the text that matched the first captured parenthesized subpattern, and so on.

You can remove the outer parentheses if the whole match is enough:

"#admin/pages(?:/\d+)?|admin/pages/add#"

Just use $matches[0].

And, as @RomanPerekhrest has written and I shamelessly include in this answer, you can shorten your pattern. You don't need to include admin/pages multiple times:

"#admin/pages(?:/add|/\d+)?#"

Upvotes: 1

Manngo
Manngo

Reputation: 16413

Try changing the order of components:

"#(admin/pages/[0-9]+|admin/pages/add|admin/pages)#"

The regular expression is satisfied as soon as something matches. In your case, it stopped as soon as it found admin/pages without looking any further.

Upvotes: 0

Related Questions