2011-05-14 98 views
0

部分我有以下URL字符串如何URL字符串分隔成使用正则表达式

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

我需要串分成部分。我想这个模式

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

但如果URL是这样/category/category1/它不能正常工作

,我需要以下瓦尔

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

如果URL是这样/category/category1/2010,我需要下列增值税

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

如果URL是这样的/category/category1/2010/12/,我需要以下瓦尔

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

如果URL是这样/category/category1/2010/12/10,我必须满足下列条件瓦尔

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

如果URL是这样/category/category1/2010/12/10/id,我需要以下瓦尔

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

是否有可能使preg_match和正则表达式?

+0

如果有人打电话'/分类/组别/ id'或任何其他组合你没有什么名单?为什么不使用普通的旧查询字符串,例如'?类别= 1&d = 2010-12-10&id'? – Gordon 2011-05-14 16:33:35

回答

1

你可以使用的preg_match。下面的正则表达式与问题中的所有测试的例子:

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

非常感谢这种模式真的有用! – Alex 2011-05-14 16:51:57

+0

@Alex不客气! – AndersTornkvist 2011-05-14 17:11:06

6

为什么不干脆explode('/', $url);

现在你已经得到了URL件的阵列。如果你也经过验证,你现在可以单独验证每个片段(存在),整个事情会更清晰。

+0

感谢您的快速回答,爆炸将分开和category/category1/category2,这将打破我的脚本,我认为 – Alex 2011-05-14 16:28:05

+0

@亚历克斯:它会以什么方式打破你的脚本? – 2011-05-14 16:30:24

+0

我认为是来自如果我有问题,例如类别1234的网址将看起来像/ 1234/2001年,当我检查有年份,我会检测2001年,但是当我试图访问类别/ 1234脚本也许试图得到1234 – Alex 2011-05-14 16:38:25

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); 

输出:

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 
1

简单的列表()函数

<?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; 

?> 
相关问题