The m is for maus~. I do what I please. About m~, The Archives and the Lifestream RSS

PHP UTF8 substring

Unfortunately, PHP’s own substr messes up the character encoding for UTF8 strings. Here’s one way to preserve the enconding.

function utf8_substr ($str, $start) {
   preg_match_all("/./u", $str, $ar);

   if (func_num_args() >= 3) {
       $end = func_get_arg(2);
       return join("",array_slice($ar[0],$start,$end));
   } else {
       return join("",array_slice($ar[0],$start));