I have a problem with a substring function. When a special char is the last char of the resulting substring (and only then!!!), this chars gets represented as an icon with a question mark in it.
Here comes my code:
$string = 'This is a string and when a German word with a special char like "Tür" appears and the special char ü is the last character of the substring it gets represented as an icon with question mark';
echo substr($string,0,102). "...";
Result:
This is a string and when a German word with a special char like "Tür" appears and the special char �...
How can I avoid this?
Sounds like you are using 8bit string functions to process unicode characters. That cannot work.
You should install the mbstring
package and enable "mbstring function overloading" in your php configuration. That will take care to silently override all relevant string handling functions to use their multi byte safe equivalents instead, so that you do not have to change your code.
There are a number of configuration options for that "mbstring" extension. Check your php.ini
configuration file and look through them. You will find the mbstring.func_overload
command which you probably want to set to the value 7
, so that all such functions are overridden. After changing that configuration you have to restart your http server process, so that the new configuration gets loaded. You can also check that by using the famous phpinfo()
function in a test script.
An alternative would be not to configure this overriding automagic, but to manually port your code to directly use those functions. For that you will have to replace all string function calls in your code by their equivalent function names. So for example mb_substr(...)
instead of just substr(...)
.
Also you really want to start reading the documentation of the tools you use. Here the introduction to those "multi byte string" functions is of interest. It should help you to understand what this all is about and what you have to take care of: http://php.net/manual/en/book.mbstring.php