Re: trimming spaces from multibyte strings

From: Date: Sat, 03 May 2003 09:32:28 +0000
Subject: Re: trimming spaces from multibyte strings
References: 1  Groups: php.i18n 
Request: Send a blank email to php-i18n+get-550@lists.php.net to get a copy of this message
Hi, Jean-Christian Imbeault <jc@mega-bucks.co.jp> wrote: > #1 is trim() multibyte safe? Principally no, but when it comes to half-width white spaces, trim() works without problem. > #2 if not what is a simple way of trimming whitespace from multibyte > strings, in general if possible, and in particular for japanese strings? 1. Enable mbregex extension at configure time and use mb_ereg_replace(). Note that mb_ereg_replace() can only handle strings encoded in SJIS, EUC-JP, UTF-8, and ASCII code sets. ex. <?php function my_mb_trim($str, $spc) { return mb_ereg_replace("[$spc]*$", "", mb_ereg_replace("^[$spc]*", "", $str)); } ?> 2. Use preg_replace with U option, assuming UTF-8 as the internal character encoding. This method is considered to be safer and stable than mb_ereg_replace() because it was reported that mb_ereg_replace() caused segmentation fault with some complicated pattern. 3. Request the developers adding a multibyte-safe version of trim(). This should definitely take some time :) Moriyoshi

« previous php.i18n (#550) next »