2013-01-14 30 views
0

我嘗試使用以下功能安全URL,但它不適用於阿拉伯語。如何修復它?爲URL安全放置標題別名

DEMO Here

<?php 
    function stringURLSafe($string) 
{ 
    // remove any '-' from the string since they will be used as concatenaters 
    $str = str_replace('-', ' ', $string); 


    // Trim white spaces at beginning and end of alias and make lowercase 
    $str = trim(strtolower($str)); 

    // Remove any duplicate whitespace, and ensure all characters are alphanumeric 
    $str = preg_replace('/(\s|[^A-Za-z0-9\-])+/', '-', $str); 

    // Trim dashes at beginning and end of alias 
    $str = trim($str, '-'); 

    return $str; 
} 
echo stringURLSafe('موقع إخباري: مرتبط بقناة {العربية} يقدم (على) مدار'); 

我想在輸出爲:

موقع-إخباري-مرتبط-بقناة-العربية-يقدم-على-مدار 
+5

雖然這不是一個有效的URL。你必須'urlencode()'字符串。 [URL中的Unicode字符](http://stackoverflow.com/q/2742852) –

回答

0

你的正則表達式改成這樣一個:
$str = preg_replace('/(\s|[^\p{Arabic}\w0-9\-])+/u', '-', $str);
這導致:
موقع-إخباري-مرتبط-بقناة-العربية-يقدم-على-مدار