为URL安全标题别名

时间:2013-01-14 08:52:18

标签: php regex

我使用以下功能尝试该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('موقع إخباري: مرتبط بقناة {العربية} يقدم (على) مدار');

我希望输出为:

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

1 个答案:

答案 0 :(得分:0)

将你的正则表达式改为这一个:
$str = preg_replace('/(\s|[^\p{Arabic}\w0-9\-])+/u', '-', $str);
结果如下:
موقع-إخباري-مرتبط-بقناة-العربية-يقدم-على-مدار

相关问题