我從MySQL db,varchar(255)utf8_general_ci字段獲取數據並嘗試使用PHP將文本寫入PDF。我需要確定PDF中的字符串長度以限制表格中文本的輸出。但我注意到mb_substr
/substr
的輸出真的很奇怪。使用UTF-8和mb_substr破壞數據
例如:
mb_internal_encoding("UTF-8");
$_tmpStr = $vfrow['title'];
$_tmpStrLen = mb_strlen($vfrow['title']);
for($i=$_tmpStrLen; $i >= 0; $i--){
file_put_contents('cutoffattributes.txt',$vfrow['field']." ".$_tmpStr."\n",FILE_APPEND);
file_put_contents('cutoffattributes.txt',$vfrow['field']." ".mb_substr($_tmpStr, 0, $i)."\n",FILE_APPEND);
}
輸出該:
數據庫:
我的問題是額外角色從哪裏來?
你不提供編碼mb_substr;你確定它獲得了正確的編碼嗎?看到[這個答案](http://stackoverflow.com/questions/13953248/php-mb-substr-not-working-correctly),以及。 – xathien
您使用mb_strlen()/ mb_substr()而不是strlen()/ substr(),因爲它可以在中間切割多字節字符,這是正確的。甚至mb_strlen()/ mb_substr()可以做的事情是在中間分割一個複合序列,如「n」和頂部的重音。您可能會將內容轉碼爲非複合形式,其中存在重音字母。 –
在變量設置後,你能向我們顯示'bin2hex($ _tmpStr)'的輸出嗎? – Michas