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?
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.
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.');
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
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;
?>
精彩评论