PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Mon May 29, 2017 10:32 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Matches in url regex
PostPosted: Tue Oct 07, 2014 12:04 pm 
Offline
Forum Newbie

Joined: Tue Oct 07, 2014 11:58 am
Posts: 2
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 :)


Top
 Profile  
 
 Post subject: Re: Matches in url regex
PostPosted: Tue Oct 07, 2014 12:51 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6243
Location: Montreal, Canada
explode()?

_________________
Supported PHP versions No longer supported versions


Top
 Profile  
 
 Post subject: Re: Matches in url regex
PostPosted: Fri Oct 10, 2014 3:56 am 
Offline
Forum Newbie

Joined: Tue Oct 07, 2014 11:58 am
Posts: 2
Celauran wrote:

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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group