2
我想知道如何實現正則表達式到下面的語句:Codeigniter - if語句中的正則表達式?
if($this->uri->segment(3) !== 'a,b,c...') {
redirect 'tools/glossary';
}
我需要檢查的URI段中只有一個長度,且只按字母順序排列。
如果沒有,則重定向到詞彙表頁面。
這怎麼可能?
我想知道如何實現正則表達式到下面的語句:Codeigniter - if語句中的正則表達式?
if($this->uri->segment(3) !== 'a,b,c...') {
redirect 'tools/glossary';
}
我需要檢查的URI段中只有一個長度,且只按字母順序排列。
如果沒有,則重定向到詞彙表頁面。
這怎麼可能?
正如其他人已經回答,preg_match將爲你工作。然而,僅僅在替代扔,笨接受正則表達式的路由規則,所以你可能在你的config/routes.php
像
$route['somecontroller/someaction/([a-zA-Z])'] = "somecontroller/someaction/$1";
$route['somecontroller/someaction/(:any)'] = "tools/glossary";
爲了避免重定向
if (!preg_match('/^[a-z]$/i', $this->uri->segment(3))) {
redirect('tools/glossary');
}
只是指出正則表達式說什麼:^意味着字符串的開始,$表示字符串的結尾,而我意味着匹配不區分大小寫。 –
另外,呃...不應該是「如果(!preg_match」?OP想要重定向是段*不*匹配 –
你是對的,我有條件倒退。 – lanzz