2011-05-14 141 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; 

?> 
相關問題