Search code examples
regexpcre

Remainder of URL path to results array after pattern match


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:

  1. find the ID which is the next URL path section right after the pattern match for either "brand" or "profile"
  2. than split the remainder of the URL path parts after the ID into separate items in the matches array, the number of URL path parts after the ID can vary from none to multiple and the trailing slash is not always there

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!


Solution

  • 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

    • In the 1st capturing group the full match.
    • In the 2nd capturing group your tag (brand or profile)
    • In the 3rd capturing group we have the ID (the number)
    • And in the 4th capturing group the URL reminder

    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.