我有一些困难,处理只为网址生成英文字符的cms,我需要让它适合葡萄牙,一个处理特殊字符的拉丁国家。
创建链接的功能是:
function generateSafeUrls($url = '') {
## normalize accented characters
$url = strtr($url, "\xA1\xAA\xBA\xBF\xC0\xC1\xC2\xC3\xC5\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD8\xD9\xDA\xDB\xDD\xE0\xE1\xE2\xE3\xE5\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF8\xF9\xFA\xFB\xFD\xFF\ã", "_ao_AAAAACEEEEIIIIDNOOOOOUUUYaaaaaceeeeiiiidnooooouuuyy");
## further character processing
$url = strtr($url, array("\xC4"=>"Ae", "\xC6"=>"AE", "\xD6"=>"Oe", "\xDC"=>"Ue", "\xDE"=>"TH", "\xDF"=>"ss", "\xE4"=>"ae", "\xE6"=>"ae", "\xF6"=>"oe", "\xFC"=>"ue", "\xFE"=>"th", "\ã"=>"a"));
## make sure its only english and dashes
$search = array("/[^a-zA-Z0-9\/]/", "/--+/");
$replace = array("-", "-");
$url = preg_replace($search, $replace, $url);
## return safe url
return($url);
}
我可以看到该函数用“ - ”替换空格,如果函数将“ç”替换为“ç”,将“ã”替换为“a”等等,那将会很棒。问题是,我对如何制作它没有任何线索。
我不想让你像我一直说的那样工作,给我提示和提示,我将非常感激;)
亲切的问候你们所有人!谢谢!
答案 0 :(得分:1)
此功能应该已经这样做了。 \ xE7是“ç”
的ascii答案 1 :(得分:1)
这是我的:
/**
* Creates a URL friendly string.
*
* Takes a "title" string as input and creates a human-friendly URL string with either
* a dash or an underscore as the word separator.
*
* @param string
* @param string dash or underscore
* @param bool
* @return string
*/
if (!function_exists('friendlyUrl')) {
function friendlyUrl($string, $separator = 'dash', $lowercase = TRUE) {
if ($separator == 'dash') {
$search = '_';
$replace = '-';
}
else {
$search = '-';
$replace = '_';
}
if (function_exists('convertAccentsAndSpecialToNormal')) {
$string = convertAccentsAndSpecialToNormal($string);
}
$trans = array(
'/&\#\d+?;/i' => '',
'/&\S+?;/i' => '',
'/\.+/i' => '',
'/\s+/' => $replace,
'/\/+/' => $replace,
'/[^a-z0-9\-\._]/i' => '',
'/'. $replace .'+/' => $replace,
'/'. $replace .'$/' => $replace,
'/^'. $replace .'/' => $replace,
'/\.+$/' => ''
);
$string = strip_tags($string);
$string = preg_replace(array_keys($trans), array_values($trans), $string);
if ($lowercase === TRUE) {
$string = strtolower($string);
}
return trim(stripslashes($string));
}
}
/**
* 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', 'ğ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'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;
}