我正在用PHP创建一个小留言板,我需要"简化"主题标题,以便在主题的网址中显示它.
例子:
Ceci est un sujet d'exemple 变 ceci-est-un-sujet-d-exemple
J'ai été à la plage 变 j-ai-ete-a-la-plage
I dislike sp&cial character$成为i-dislike-spcial-character(如果有更好的翻译,我会接受)
编辑1:所有字符串必须是UTF-8编码
编辑2:我最近了解到这种操作的结果被称为"slug"或"友好网址"
这就是我所做的.我想我已经涵盖了大多数"常见"角色.显然,它会丢失一些.
/**
* Replaces special characters in a string with their "non-special" counterpart.
*
* Useful for friendly URLs.
*
* @param string
* @return string
*/
function convertAccentsAndSpecialToNormal($string) {
$table = array(
'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', '?'=>'A', '?'=>'A', '?'=>'A', 'Æ'=>'A', '?'=>'A',
'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', '?'=>'a', '?'=>'a', '?'=>'a', 'æ'=>'a', '?'=>'a',
'Þ'=>'B', 'þ'=>'b', 'ß'=>'Ss',
'Ç'=>'C', '?'=>'C', '?'=>'C', '?'=>'C', '?'=>'C',
'ç'=>'c', '?'=>'c', '?'=>'c', '?'=>'c', '?'=>'c',
'?'=>'Dj', '?'=>'D', '?'=>'D',
'?'=>'dj', '?'=>'d',
'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', '?'=>'E', '?'=>'E', '?'=>'E', '?'=>'E',
'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', '?'=>'e', '?'=>'e', '?'=>'e', '?'=>'e',
'?'=>'G', '?'=>'G', '?'=>'G', '?'=>'G',
'?'=>'g', '?'=>'g', '?'=>'g', '?'=>'g',
'?'=>'H', '?'=>'H',
'?'=>'h', '?'=>'h',
'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', '?'=>'I', '?'=>'I', '?'=>'I', '?'=>'I', '?'=>'I',
'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', '?'=>'i', '?'=>'i', '?'=>'i', '?'=>'i', '?'=>'i',
'?'=>'J',
'?'=>'j',
'?'=>'K',
'?'=>'k', '?'=>'k',
'?'=>'L', '?'=>'L', '?'=>'L', '?'=>'L', '?'=>'L',
'?'=>'l', '?'=>'l', '?'=>'l', '?'=>'l', '?'=>'l',
'Ñ'=>'N', '?'=>'N', '?'=>'N', '?'=>'N', '?'=>'N',
'ñ'=>'n', '?'=>'n', '?'=>'n', '?'=>'n', '?'=>'n', '?'=>'n',
'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', '?'=>'O', '?'=>'O', '?'=>'O', 'Œ'=>'O',
'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', '?'=>'o', '?'=>'o', '?'=>'o', 'œ'=>'o', 'ð'=>'o',
'?'=>'R', '?'=>'R',
'?'=>'r', '?'=>'r', '?'=>'r',
'Š'=>'S', '?'=>'S', '?'=>'S', '?'=>'S',
'š'=>'s', '?'=>'s', '?'=>'s', '?'=>'s',
'?'=>'T', '?'=>'T', '?'=>'T',
'?'=>'t', '?'=>'t', '?'=>'t',
'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', '?'=>'U', '?'=>'U', '?'=>'U', '?'=>'U', '?'=>'U', '?'=>'U',
'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'u', '?'=>'u', '?'=>'u', '?'=>'u', '?'=>'u', '?'=>'u', '?'=>'u',
'?'=>'W', '?'=>'W', '?'=>'W', '?'=>'W',
'?'=>'w', '?'=>'w', '?'=>'w', '?'=>'w',
'Ý'=>'Y', 'Ÿ'=>'Y', '?'=>'Y',
'ý'=>'y', 'ÿ'=>'y', '?'=>'y',
'Ž'=>'Z', '?'=>'Z', '?'=>'Z', 'Ž'=>'Z',
'ž'=>'z', '?'=>'z', '?'=>'z', 'ž'=>'z',
'“'=>'"', '”'=>'"', '‘'=>"'", '’'=>"'", '•'=>'-', '…'=>'...', '—'=>'-', '–'=>'-', '¿'=>'?', '¡'=>'!', '°'=>' degrees ',
'¼'=>' 1/4 ', '½'=>' 1/2 ', '¾'=>' 3/4 ', '?'=>' 1/3 ', '?'=>' 2/3 ', '?'=>' 1/8 ', '?'=>' 3/8 ', '?'=>' 5/8 ', '?'=>' 7/8 ',
'÷'=>' divided by ', '×'=>' times ', '±'=>' plus-minus ', '?'=>' square root ', '?'=>' infinity ',
'?'=>' almost equal to ', '?'=>' not equal to ', '?'=>' identical to ', '?'=>' less than or equal to ', '?'=>' greater than or equal to ',
'?'=>' left ', '?'=>' right ', '?'=>' up ', '?'=>' down ', '?'=>' left and right ', '?'=>' up and down ',
'?'=>' care of ', '?' => ' estimated ',
'?'=>' ohm ',
'?'=>' female ', '?'=>' male ',
'©'=>' Copyright ', '®'=>' Registered ', '™' =>' Trademark ',
);
$string = strtr($string, $table);
// Currency symbols: £¤¥€ - we dont bother with them for now
$string = preg_replace("/[^\x9\xA\xD\x20-\x7F]/u", "", $string);
return $string;
}
/**
* Create URL Title
*
* Takes a "title" string as input and creates a human-friendly URL string.
*
* @param string
* @param boolean
* @return string
*/
if (!function_exists('friendlyUrl')) {
function friendlyUrl($string, $lowercase = TRUE) {
$separator = '-';
if (function_exists('convertAccentsAndSpecialToNormal')) {
$string = convertAccentsAndSpecialToNormal($string);
}
$trans = array(
'/&\#\d+?;/i' => '',
'/&\S+?;/i' => '',
'/\.+/i' => '',
'/\s+/' => $separator,
'/\/+/' => $separator,
'/[^a-z0-9\-\._]/i' => '',
'/'. $separator .'+/' => $separator,
'/'. $separator .'$/' => $separator,
'/^'. $separator .'/' => $separator,
'/\.+$/' => ''
);
$string = strip_tags($string);
$string = preg_replace(array_keys($trans), array_values($trans), $string);
if ($lowercase === TRUE) {
$string = strtolower($string);
}
return trim(stripslashes($string));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1275 次 |
| 最近记录: |