所以,这就是问题所在,我觉得这个功能一切正常,但网址如下:
reparações
总是显示为“reparaa-a-es”。
预期结果应为“遣返”
编辑:
所以,解决方案很简单,似乎php具有本机功能 做我需要的:
rawurlencode ( string $str )
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");
## make sure its only english and dashes
$url3 = preg_replace("/[^A-Za-z0-9_-]+/", "_", $url);
## return safe url
return($url3);
}
应该是什么问题?
谢谢你们。
答案 0 :(得分:1)
尝试此操作(使用UTF8解码而不是尝试声明所有十六进制):
function generateSafeUrls($url = ''){
$bad = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿŔŕ';
$good = 'aaaaaaaceeeeiiiidnoooooouuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr';
$url= strtr(utf8_decode($url), utf8_decode($bad), $good);
return preg_replace("/[^A-Za-z0-9_-]+/", "_", $url);
}
然而,正如Conrad Shultz在评论中指出的那样,你不一定需要这样做......简单的urlencode()
应该没问题......如果你想要看起来更清晰的网址,你应该运行{{ 1}}以便网址没有preg_replace
等,您应该%20
答案 1 :(得分:1)
$GLOBALS['normalizeChars'] = array(
'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f'
);
function cleanForShortURL($toClean) {
$toClean = str_replace('&', '-and-', $toClean);
$toClean = trim(preg_replace('/[^\w\d_ -]/si', '', $toClean));//remove all illegal chars
$toClean = str_replace(' ', '-', $toClean);
$toClean = str_replace('--', '-', $toClean);
return strtr($toClean, $GLOBALS['normalizeChars']);
}
答案 2 :(得分:0)
function slug($string, $replacement = '-') {
$map = array(
'/à|á|å|â/' => 'a',
'/è|é|ê|ẽ|ë/' => 'e',
'/ì|í|î/' => 'i',
'/ò|ó|ô|ø/' => 'o',
'/ù|ú|ů|û/' => 'u',
'/ç/' => 'c',
'/ñ/' => 'n',
'/ä|æ/' => 'ae',
'/ö/' => 'oe',
'/ü/' => 'ue',
'/Ä/' => 'Ae',
'/Ü/' => 'Ue',
'/Ö/' => 'Oe',
'/ß/' => 'ss',
'/[^\w\s]/' => '',
'/\\s+/' => $replacement,
"/$replacement+/" => $replacement
);
return preg_replace(array_keys($map), array_values($map), $string);
}
答案 3 :(得分:0)
我认为你想要的是音译人物。也许这样的事情可能有所帮助:
/**
* Strips non-url friendly characters from $string
*
* @param string $url string to be checked
* @return string cleaned url
*/
function generateSafeUrls($url)
{
$url = iconv('UTF-8', 'US-ASCII//TRANSLIT', $url);
return preg_replace('~[^a-z0-9_\.-]|\s+~i', '_', $url);
}
echo generateSafeUrls('reparações');
// reparacoes