Simple PHP Regex
I am setting up a Zend_Route (but it is still just a regex) and I wish to match a url like
/en/experience/this-is-my-name-and-the-last-is-1-of-id-123456.html
So I want to grab the
this-is-my-name-and-the-last-i开发者_开发问答s-1-of
and the
123456
I tried
\w{2}/experience/(.+)?-(\d+)\.html
but that doesn't seem to work.
It would be easy if the other way around e.g. if it was id the name
/en/experience/123456-this-is-my-name-and-the-last-is-1-of-id.html
I could use
\w{2}/experience/(\d+)-(.+)\.html
But that is a cop out - so any advice on how to match original format?
Try this one:
/\w{2}/experience/(.+?)-(\d+)\.html
try this:
/\w{2}/experience/(.+)?-(\d+)\.html
zend route internally does this:
preg_match('#^/\w{2}/experience/(.+)?-(\d+)\.html$#i', '/en/experience/this-is-my-name-and-the-last-is-1-of-id-123456.html', $matches);
so, your pattern only matches with a slash on the beginning.
精彩评论