I'm having difficulties to get my regex to do what I want it to do :(
I want my regex to do the following 2 things:
I managed to get the first part working via:
<?php
$url = 'https://demo.com/show/profile/123/slug/etc/';
$pattern = '/\/(brand|profile)?\/([\d]+)/';
preg_match($pattern, $url, $matches);
var_dump($matches);
Which works for all of these test strings:
https://demo.com/show/profile/123
https://demo.com/show/profile/123/
https://demo.com/show/profile/123/slug
https://demo.com/show/profile/123/slug/
https://demo.com/show/profile/123/slug/etc
https://demo.com/show/profile/123/slug/etc/
But I don't seem to be able to solve the second part, even after searching for a solution for days. My most "successful" attempt till now is:
\/(brand|profile)?\/([\d]+)\/?(.*)?\/?
That pattern captures the remainder of the URL path all together including the trailing slash when it's present (btw - I don't want the trailing slash).
I've the code for the first part available on: phpliveregex.com/p/pMO
Can someone help me how to extend that with the code for the second part?
Much appreciated!
I suggest to split this problem in two sub-problems.
I mean.
Could we do a first preg_match
with this regex
(brand|profile)\/(\d+)(.*)
Here we have
Then with the full URL reminder (4th capturing group) we could do a preg_match_all
with this regex
[^\/]+
And here you have all the reminder paths.