Re: trimming spaces from multibyte strings

Moriyoshi Koizumi <[email protected]>
Newsgroups gmane.comp.php.internationalization
Message-ID <[email protected]>
Hi,

Jean-Christian Imbeault <[email protected]> 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


-- 
PHP Internationalization Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.