0

I have following URL string

/category/category1/2010/12/10/id

I need to separate string into parts. I'm trying this pattern

(?:(.*))?(?:(?:\/)?([0-9]{4}))?(?:(?:\/)?([0-9]{1,2}))?(?:(?:\/)?([0-9]{1,2}))?(?:(.*))?

but it doesn't work properly

if URL is like this /category/category1/, I need the following vars

path = '/category/category1/';
year = '';
month = '';
day = '';
id ='';

if URL is like this /category/category1/2010, I need the following vars

path = '/category/category1/2010';
year = '2010';
month = '';
day = '';
id ='';

if URL is like this /category/category1/2010/12/, I need the following vars

path = '/category/category1/2010/12';
year = '2010';
month = '12';
day = '';
id ='';

if URL is like this /category/category1/2010/12/10, I need the following vars

path = '/category/category1/2010/12/10';
year = '2010';
month = '12';
day = '10';
id ='';

if URL is like this /category/category1/2010/12/10/id, I need the following vars

path = '/category/category1/2010/12/10/id';
year = '2010';
month = '12';
day = '10';
id ='id';

Is it possible to make this with preg_match and regex?

1
  • what if someone calls /category/category1/id or any other combination you didnt list? Why not just use plain old query strings instead, e.g. ?category=1&d=2010-12-10&id? Commented May 14, 2011 at 16:33

4 Answers 4

6

Why not simply explode('/', $url);

Now you've got an array of the URL pieces. If you're also after validation you can now validate each piece (that exists) individually, and the whole thing will be a lot clearer.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for fast answer, explode will separate and category/category1/category2 and this going to break my script i think
@Alex: In what way would it break your script?
the problem i think is coming from if i have, for example category 1234 the url will look like /1234/2001 when i check for there is year i will detect year 2001 but when i trying to access to category /1234 the script perhaps trying to get year 1234
1

You could use preg_match. The following regex is tested with all examples in the question:

$regex = '/^(\\/[^\\/]+\\/[^\\/]+\\/)\\/?([0-9]{4})?\\/?([0-9]{2})?\\/?([0-9]{2})?\\/?(.*)$/';
if (preg_match($regex, $url, $match))
    print_r($match);
else
    die('No match.');

Comments

1

PHP:

function findInfo ($path) {
    return array_slice(explode('/', trim($path, '/')), 2);
}

$path = "/category/category1/2010/12/10/id";
list($year, $month, $day, $id) = findInfo($path);

var_dump($path, $year, $month, $day, $id);

$path = "/category/category1/";
list($year, $month, $day, $id) = findInfo($path);

var_dump($path, $year, $month, $day, $id);

$path = "/category/category1/2010/";
list($year, $month, $day, $id) = findInfo($path);

var_dump($path, $year, $month, $day, $id);

$path = "/category/category1/2010/12";
list($year, $month, $day, $id) = findInfo($path);

var_dump($path, $year, $month, $day, $id);

$path = "/category/category1/2010/12/10";
list($year, $month, $day, $id) = findInfo($path);

var_dump($path, $year, $month, $day, $id);

Output:

string(33) "/category/category1/2010/12/10/id"
string(4) "2010"
string(2) "12"
string(2) "10"
string(2) "id"
string(20) "/category/category1/"
NULL
NULL
NULL
NULL
string(25) "/category/category1/2010/"
string(4) "2010"
NULL
NULL
NULL
string(27) "/category/category1/2010/12"
string(4) "2010"
string(2) "12"
NULL
NULL
string(30) "/category/category1/2010/12/10"
string(4) "2010"
string(2) "12"
string(2) "10"
NULL

Comments

1

simple list() function

<?php 
$url = 'http://example.com/category/category1/2010/12/10/id';
$url = str_ireplace('http://example.com/','',$url);
/*
category/category1/2010/12/10/id
=
//type/cat/year/month/day/id
using list()
*/
list($type,$cat,$year, $month, $day,$id) = explode('/',$url); 

echo $id.':'.$day.'-'.$month.'-'.$year;

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.