PHP Developers Network
http://forums.devnetwork.net/

Matches in url regex
http://forums.devnetwork.net/viewtopic.php?f=38&t=140368
Page 1 of 1

Author:  xpc234 [ Tue Oct 07, 2014 12:04 pm ]
Post subject:  Matches in url regex

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:

Syntax: [ Download ] [ Hide ]
<?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 :)

Author:  Celauran [ Tue Oct 07, 2014 12:51 pm ]
Post subject:  Re: Matches in url regex

?

Author:  xpc234 [ Fri Oct 10, 2014 3:56 am ]
Post subject:  Re: Matches in url regex


Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/