phpregexpreg-match

PHP CMS URL Pattern Matching


As mentioned in the title, I'm building a CMS and I'm trying to work Patterned URLs into the system. As part of this, Modules can be assigned to a base handle, then have parameters passed to it.

Using a regular expression I'd like to match URLs and be able to extract the wildcard information from them. The general format is below.

http://example.com/module/
http://example.com/module/action/
http://example.com/module/action/id/
http://example.com/module/action/id/subaction/

I'm currently running this by having 4 regular expressions for each module (that has this feature enabled) are loaded from the database and individually checked.

The problem that I'm having is that the expressions are matching the first, then not continuing through the order.

This is the expression that I'm using at the moment for the first level.

/^module\//

Any help is greatly appreciated!


Solution

  • I'm using /^photography$/, /^photography\/([a-zA-Z0-9_-])*$/, and /^photography\/([a-zA-Z0-9_-])*\/([a-zA-Z0-9_-])*/ to confirm which module to use (with another ([a-zA-Z0-9_-])*\/ for each level deeper, then /^photography\/(.*?)/ and /^proofs\/(.*?)\/(.*?)/ to get the 'action' and 'id(additionally with another/(.*?)/` for each sub action deeper. It's all processed in PHP - I get all expressions from the database then loop through them.