Reputation: 1686
I'm using the following code to split incoming requests:
/*
* Create global $GET from our cool URL's
*/
preg_match_all('/[\/|\?|\&]([a-zA-Z\_0-9]+)([\/|=]([0-9]+)){0,1}/', $_SERVER['REQUEST_URI'], $parts, PREG_PATTERN_ORDER);
if( count($parts[1]) >= 1 && count($parts[3]) >= 1)
$GET = array_combine($parts[1], $parts[3]);
URL's like "/cat/2/post/345/answer/post" is divided into:
cat => 2
post => 345
answer =>
post =>
is it possible to change this regex to allow for urls like: /post/345-title-goes-here/ and make it into:
post => 345-title-goes-here
becasue right now it only works if the argument is strictly 0-9 with no other characters. and even that was a real problem for me. any suggestions? :)
Upvotes: 0
Views: 152
Reputation: 91373
I'd use a more concise one:
#[/?&](\w+)([/=](\d[\w-]+))?#
so the preg_match_all
becomes:
preg_match_all('#[/?&](\w+)([/=](\d[\w-]+))?#', $_SERVER['REQUEST_URI'], $parts, PREG_PATTERN_ORDER);
Update according to comment:
#[/?&]([\w.()-]+)([/=](\d[\w.()-]+))?#
You can add every character you want within the character class.
Upvotes: 1
Reputation: 631
Sure, instead of:
'/[\/|\?|\&]([a-zA-Z\_0-9]+)([\/|=]([0-9]+)){0,1}/'
try:
'/[\/|\?|\&]([a-zA-Z\_0-9]+)([\/|=]([0-9]+[a-zA-Z_-]*)){0,1}/'
I just added the extra characters after the 0-9 in an optional (0 or more) set.
Upvotes: 1