On 6/12/09 11:50 AM, "Tom Worster" <f...@thefsb.org> wrote:

> say a table in the db has a varchar(255) column, 255 being the max number of
> octets of strings that can go in the column. now say the php script very
> occasionally has to deal with utf8 input strings with octet length > 255 -- it
> needs to select rows matching the input string or insert the input string.
> 
> so what i think i need is a function to truncate a utf8 string to the longest
> valid utf8 string that has octet length <= 255.
> 
> is this what mb_strcut() is for? i'm having a hard time understanding the man
> page for that function.

i satisfied myself that mb_cutstr() probably does what i need with the
following:

$default_locale = setlocale(LC_ALL, 'en_US.UTF-8');
ini_set('default_charset', 'UTF-8' );

$strs = array(
    'Iñtërnâtiônàlizætiøn',
    'החמאס: רוצים להשלים את עסקת שליט במהירות האפשרית',
    'ايران لا ترى تغييرا في الموقف الأمريكي',
    '独・米で死傷者を出した銃の乱射事件',
    '國會預算處公布驚人的赤字數據後',
    '이며 세계 경제 회복에 걸림돌이 되고 있다',
    'В дагестанском лесном массиве южнее села Какашура',
    'นายประสิทธิ์ รุ่งสะอาด ปลัดเทศบาล รักษาการแทนนายกเทศมนตรี
ต.ท่าทองใหม่',
    'ભારતીય ટીમનો સુવર્ણ યુગ : કિવીઝમાં પણ કમાલ',
    'ཁམས་དཀར་མཛེས་ས་ཁུལ་དུ་རྒྱ་གཞུང་ལ་ཞི་བའི་ངོ་རྒོལ་',
    'Χιόνια, βροχές και θυελλώδεις άνεμοι συνθέτουν το',
    'Հայաստանում սկսվել է դատական համակարգի ձեւավորումը',
    'რუსეთი ასევე გეგმავს სამხედრო');

foreach ( $strs as $s ) {

    for ( $i=10; $i<100; $i+=10) {
        $t = mb_strcut($s, 0, $i);
        $ok = mb_check_encoding($t, 'UTF-8') ? 'OK' : 'Bad';
        print("$i\t" . mb_strlen($t, 'ISO-8859-1') . "\t"
            . mb_strlen($t, 'UTF-8') . "\t$ok\t$t\n");
    }

}



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to