I'm get data from MySQL db, varchar(255) utf8_general_ci field and try to write the text to a PDF with PHP. I need to determine the string length in the PDF to limit the output of the text in a table. But I noticed that the output of mb_substr
/substr
is really strange.
For example:
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);
}
outputs this:
npp file link
Database:
My question is where does the extra character come from?
You need to tell your mb_
functions that the data is in UTF-8 so they can treat it correctly. Either set this globally for all functions using mb_internal_encoding
, or pass the $encoding
parameter to your function when you call it:
mb_substr($_tmpStr, 0, $i, 'UTF-8')
The extra character is first part of two byte UTF-8 sequence. You may have problems with internal encoding of Multibyte String Functions. Your code treats text as fixed, 1-byte encoding. The ń in UTF-8, hex C5 84, is treated as Ĺ„ in CP-1250 and Ĺ[IND] in ISO-8859-2, two characters.
Try to execute this one on the top of script:
mb_internal_encoding("UTF-8");
http://php.net/manual/en/function.mb-internal-encoding.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With