2015-09-28 97 views
3

我希望我的路由支持波斯字符,但是當在正則表達式路徑中使用x {600} - \ x {6FF}時,應用會給我一個404錯誤(找不到頁面)zend框架2使用UTF-8正則表達式路由

我ZF2路由配置:

'result' => array(
       'type' => 'segment', 
       'options' => array(     
        'route' => '/Result[/:name]', 
        'constraints' => array(
         'name' => '[\sa-zA-Z0-9_.-x{600}-\x{6FF}]*', 

        ), 
        'defaults' => array(
         'controller' => 'Result', 
         'action'  => 'index', 
        ), 
       ), 
      ), 

,我添加u結束在Zend的\的mvc \路由器正則表達式的\ HTTP \ Segment.php

if ($pathOffset !== null) { 
      $result = preg_match('(\G' . $regex . ')u', $path, $matches,null, $pathOffset); 
     } else { 
      $result = preg_match('(^' . $regex . '$)u', $path, $matches); 
     } 

我的網址是這樣的:

http://localhost/test/Result/A.8.کلمه 
+0

嘗試''[\ sa-zA-Z0-9 _。\ x {600} - \ x {6FF} - ] *''。 –

+0

我嘗試使用上面的代碼,但給我404錯誤 –

+0

看來你需要將'rawurldecode($ path);'傳遞給'preg_match'。請嘗試使用上述正則表達式。 –

回答

0

在字符類問題中有一個錯位的連字符,在第一個x{600}之前缺少\。這裏是如何的hyphen changes the meaning of the pattern

enter image description here

要解決它,你需要把它移動到字符類的末尾:

'[\sa-zA-Z0-9_.\x{600}-\x{6FF}-]*' 

another demo

請注意,如果您需要任何更多的字符添加到模式,你應該小心[]-/。他們必須逃避以避免問題。例如,加入%

'[%\sa-zA-Z0-9_.\x{600}-\x{6FF}-]*' 

接下來的問題是,你需要傳遞rawurldecode($path)preg_match,而不是僅僅$path

+0

很高興爲你效勞。如果我的答案對您有幫助,請考慮也是upvoting(請參閱[如何在堆棧溢出?](http://meta.stackexchange.com/questions/173399/how-to-upvote-on-stack-overflow)) 。 –