Page 1 of 1

Matches in url regex

Posted: Tue Oct 07, 2014 12:04 pm
by xpc234
Hi :)
I want to parse any url that separates string with '/'
For example: /home/index/3
or /users/16

So i wrote the following code:

Code: Select all

<?php

$url = Some input...;

$regex = "(/([a-zA-Z0-9_]+))+";

if(preg_match("@^" . $regex . "$@", $url, $matches)) {
	echo "<pre>";
	print_r($matches);
	echo "</pre>";
}

?>
I expect the result for /home/index/3 to be:

array (0 => "home", "1" => "index", 2 => "3")

But the actual result is: array (0 => "/home/index/3", 1 => "/3", 2 => "3")
It completely ignores home and index...

Can someone tell me please what i had done wrong?
Thanks :)

Re: Matches in url regex

Posted: Tue Oct 07, 2014 12:51 pm
by Celauran

Re: Matches in url regex

Posted: Fri Oct 10, 2014 3:56 am
by xpc234
Celauran wrote:explode()?
explode will work only for simple case where the url is separated by a certain character.
I want to allow the user (Developer) to provide a general regex.

Sorry, i didn't make it clear enough in my original post.