2017-04-05 236 views
1

嘿我的路由腳本是不是與特殊的字符工作,我沒有任何想法瞭解爲什麼或如何解決它。Php preg_match不能使用?

我的網址:/test/x/hállò/123
我的路由器加入URL:/測試/:VARx前提/:variableZ/123

$route['url'] ="/test/x/:name/123"; 
$reqMet = $_SERVER['REQUEST_METHOD']; 
$route['method'] = "GET"; 
$reqUrl = "/test/x/hállò/123"; 
$matches = Array(); 

$pattern = "@^" . preg_replace('/\\\:[a-zA-Z0-9\_\-]+/', '([a-zA-Z0-9\-\_]+)', preg_quote($route['url'])) . "[email protected]"; 

if($reqMet == $route['method'] && preg_match($pattern, $reqUrl, $matches)) { 
    echo "THIS SHOULD BE THE OUTPUT"; 
} 

因此,它不能與像LOL或乾草字工作,但我回來了正確的網址,但像文章/測試/ 123的東西都是完美的。

希望有人爲我解決問題。 感謝

編輯

所以這是工作

$router->addRoute('GET','/test/x/hállò/123','home/main',false); 

URL >>http://localhost/test/x/hállò/123

但這不

$router->addRoute('GET','/test/x/:name/123','home/main',false`); 

URL >>http://localhost/test/x/hállò/123

:名稱是可替換的,像文章ID或其他任何變量。

+0

的[我怎麼能預浸\可能的複製_replace特殊字符,如'Prêt-à-porter'?](http://stackoverflow.com/questions/2050723/how-can-i-preg-replace-special-character-like-pr%c3%aat-%c3 %a0-porter)以及[preg_match an d(非英文)拉丁字符](http://stackoverflow.com/questions/5424494/preg-match-and-non-english-latin-characters) – Jer

+0

@ chris85是的但你應該得到:S我想得到it – Juan

+0

@ C0dekid nah它沒有解決我的問題 – Juan

回答

0

雖然看起來您可能需要包含其他幾個重音和/或非英文字母,但我只會包含您提到的字母。

的替代和超寬鬆的模式(也許不是足夠安全的)將\\\:[^\/]+

代碼:

$route['url'] ="/test/x/:name/123"; 
$reqUrl = "/test/x/hállò/123"; 

echo "Input:\n"; 
var_export(preg_quote($route['url'])); 

$pattern="@^".preg_replace('/\\\:[a-zA-Z0-9áò_-]+/','([a-zA-Z0-9áò_-]+)',preg_quote($route['url']))."[email protected]"; 
//           ^^ new letters here: ^^ 

if(preg_match($pattern, $reqUrl, $matches)) { 
    echo "\n\nOutput\n"; 
    var_export($matches); 
}else{ 
    echo "\n\nNo Match"; 
} 

顯示:

Input: 
'/test/x/\\:name/123' 

Output 
array (
    0 => '/test/x/hállò/123', 
    1 => 'hállò', 
) 
+0

我今天看到它:)我會測試它,但它似乎是一個穩定的好答案,並解決它。 – Juan

+0

你的mvp老兄:D! – Juan