encoding - PHP and UTF-8 String functions WITHOUT MB-Functions? -
i try use utf-8 php, output seems okay (display correct äöüß etc, when testing) on site, there problem... when use echo strlen("Ä");
shows me "2"... read topic: strlen() , utf-8 encoding in answer read this:
the replacement character gets inserted when utf-8 decoder reads data that's not valid utf-8 data.
i wonder, why data not valid utf-8? because:
- i saved files in "utf-8 no bom"
- used utf-8 header on first line
- my browser says "encoding: utf-8"
this code:
<?php header("content-type: text/html; charset=utf-8"); $test = 'Ä'; echo strlen($test); var_dump($test); ?>
my question: can use normal php-functions utf-8 or must use "mb"-functions?
if it's possible use normal php-functions, why show me strlen() 2 in code, instead of 1?
strlen()
return length of string in bytes default, not characters... can change setting mbstring.func_overload ini setting tell php return characters strlen() call instead.... global, , affects number of other functions well, strpos()
, substr()
(full list in documentation link)
this can have serious adverse effects elsewhere in code, particularly if you're using 3rd party libraries aren't aware of it, isn't recommended.
it's better use mb_*
functions if know you're working utf-8 strings... , (when comes it) setting mbstring.func_overload
telling php use mb_*
functions alternative normal string functions "under hood"
Comments
Post a Comment