Re: trimming spaces from multibyte strings

From: Date: Tue, 06 May 2003 05:58:25 +0000
Subject: Re: trimming spaces from multibyte strings
References: 1 2  Groups: php.i18n 
Request: Send a blank email to php-i18n+get-552@lists.php.net to get a copy of this message
Moriyoshi Koizumi wrote:
#1 is trim() multibyte safe?
Principally no, but when it comes to half-width white spaces, trim() works without problem.
Hum you say 'no' but then 'yes' :) By "white spaces" did you mean "whitespace" as in tabs, line feeds, etc... or specifically only ASCII 0x20? If you meant all of trim()'s whitespaces (0x[00,09,0A,0B,0D,20]) then trim() *is* multibyte safe no?
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));
       }
?>
This function is fine except that I would have to first define my internal encoding and then define what whitespace is ... I was hoping for something more generic. But then again I guess that the is no generic definition of whitespace in that different character sets have different codes for white space. Can ASCII whitespace be considered universal in all character sets? But isn't there a regular expression for whitespace? Something like 'sS'? Is using the regex patter for whitespace not an option?
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.
I would except that my internal encoding is EUC :(
3. Request the developers adding a multibyte-safe version of trim(). This should definitely take some time :)
I did ask something similar six months back but was told (by you :) that this was not a common "problem" and that asking this list would be better. But since you suggest it I will ask for this as a new feature as I really do find it difficult to remove whitespace from multibyte strings :) Thanks, Jc

« previous php.i18n (#552) next »