2017-04-05 245 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