taratula
taratula

Reputation: 497

Optional route php patterns

This is my code:

$url = '/'; //or
$url = '/slug'; //or
$url = '/slug/slug1'; //or
$url = '/slug/slug1/slug2'; //or
$url = '/slug/slug1/slug2/slug3';
$patterns = ???
preg_match_all($pattern, $url, $parts, PREG_OFFSET_CAPTURE);

What is the pattern to satisfy all the url above? Somebody can help me?

Upvotes: 0

Views: 36

Answers (2)

CodeBoy
CodeBoy

Reputation: 3300

Why use a pattern? If all you want is the parts of the url, a much simpler solution is ...

$parts = explode('/', trim($url, ' /'));

(Note that I trim leading and trailing spaces and / (forward slash) so that I don't get an empty string "" as the first or last element of the $parts array--unless $url was just "/".)

Upvotes: 0

Amarnasan
Amarnasan

Reputation: 15549

This pattern satisfy them all:

$pattern = "!^/(slug|slug(/slug\d+)+)?$!";

But of course, your question should be "Satisfy all the url above and no other", because "/" also matches them all...

Upvotes: 1

Related Questions