note 44838 modified in function.substr by aidan

From: Date: Sun, 22 Aug 2004 11:18:07 +0000
Subject: note 44838 modified in function.substr by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75094@lists.php.net to get a copy of this message
Regarding windix's function to handle UTF-8 strings: one can use the "u" modifier on the regular expression so that the pattern string is treated as UTF-8 (available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32). This way the function works for other encodings too (like Greek for example). The modified function would read like this: <?php 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)); } } ?> --was-- Regarding windix's function to handle UTF-8 strings: one can use the "u" modifier on the regular expression so that the pattern string is treated as UTF-8 (available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32). This way the function works for other encodings too (like Greek for example). The modified function would read like this: 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)); } } http://php.net/manual/en/function.substr.php

« previous php.notes (#75094) next »