I am using the following code:
preg_match('%^(\w+)(/(\w+))?$%', $pathInfo, $matches);
to parse a url string.
Can somebody can explain me what this expression parse?
Because it works great with /example, but with /example one, or /example-one doens’t work anymore for $matches[1]. Thanks!
It doesn’t work because
\wmatches alphanumeric characters plus an underscore. It will not match a hyphen and a space.Short solution: add them to a character class, turn
\winto[\w -].Long solution: use
normal* (special normal*)*to minimize the amount of backtracking, withnormalbeing\wandspecialbeing[- ]: