phpregexxslt-1.0symphony-cms

Regex match optional third url parameter


I have following URL structure:

http://sitename.com/subdir/category-name/subcategory-name/article

Here are some example URLs:

http://127.0.0.1/ocko/works/videos/
http://127.0.0.1/ocko/texts/stuff/
http://127.0.0.1/ocko/works/teletubbies/oint/
http://127.0.0.1/ocko/works/teletubbies/oint/

Category can either be works or texts and article is optional.

This is my regex:

(works|texts)(\/.+)(?:(\/.+))\/

Problem is that it only matches last 2 examples, like the third parameter (article) is mandatory even though I wrote (?:(\/.+)) for last part.

I'm trying to rerout stuff behind the scenes to something like :

http://127.0.0.1/ocko/category/works/videos/

And I'm constructing string like : http://127.0.0.1/ocko/category/$1$2$3


Solution

  • How about:

     (works|texts)(\/.+?)(\/.+?)?\/
    

    https://regex101.com/r/oK1eU7/1