Matches in url regex

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
xpc234
Forum Newbie
Posts: 2
Joined: Tue Oct 07, 2014 11:58 am

Matches in url regex

Post 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 :)
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Matches in url regex

Post by Celauran »

xpc234
Forum Newbie
Posts: 2
Joined: Tue Oct 07, 2014 11:58 am

Re: Matches in url regex

Post 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.
Post Reply