我想要實現這個函數來處理乾淨的URL爲我的網頁:http://cubiq.org/the-perfect-php-clean-url-generatorUTF-8字符編碼的麻煩
當我使用它像這樣它工作正常的任何字符:
echo toAscii('åäö');
但只要我(與「AAO」再次)形式像這樣測試它從輸入:
if (isset($_POST['test'])) {
$test = $_POST['test'];
}
echo toAscii($test);
我得到以下錯誤:注意:的iconv()[function.iconv]:檢測到非法字符輸入字符串在C:\ XAMPP \ htdocs中\網絡\ bsCMS \ ysmt \上線testurl.php 12
這是完整的函數toascii將:
setlocale(LC_ALL, 'en_US.UTF8');
function toAscii($str, $replace=array(), $delimiter='-') {
if(!empty($replace)) {
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
我的猜測是我必須從同步的字符編碼toAscii函數的形式但是如何?
嘗試一下'$ con> set_charset(「utf8」);'這對我來說一直有效。假設數據庫連接'$ con = mysqli_connect(「xxx」,「xxx」,「xxx」,「xxx」);' –
嘗試使用'header('Content-type:text/html; charset = utf-8') ;'在你的php之上,並在html中使用'' –
@ Fred-ii-與MySQL連接無關。這是'iconv()'函數調用失敗。 – ComFreek