note 92665 modified in function.mb-split by danbrown

From: Date: Mon, 03 Aug 2009 16:45:08 +0000
Subject: note 92665 modified in function.mb-split by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159035@lists.php.net to get a copy of this message
We are talking about Multi Byte ( e.g. UTF-8) strings here, so preg_split will fail for the following string: 'Weiße Rosen sind nicht grün!' And because I didn't find a regex to simulate a str_split I optimized the first solution from adjwilli a bit: <?php $string = 'Weiße Rosen sind nicht grün!' $stop = mb_strlen( $string); $result = array(); for( $idx = 0; $idx < $stop; $idx++) { $result[] = mb_substr( $string, $idx, 1); } ?> Here is an example with adjwilli's function: <?php mb_internal_encoding( 'UTF-8'); mb_regex_encoding( 'UTF-8'); function mbStringToArray ( $string ) { $stop = mb_strlen( $string); $result = array(); for( $idx = 0; $idx < $stop; $idx++) { $result[] = mb_substr( $string, $idx, 1); } return $result; } echo '<pre>', PHP_EOL, print_r( mbStringToArray( 'Weiße Rosen sind nicht grün!', true)), PHP_EOL, '</pre>'; ?> Let me know [by personal email], if someone found a regex to simulate a str_split with mb_split. --was-- We are talking about Multi Byte ( e.g. UTF-8) strings here, so preg_split will fail for the following string: 'Weiße Rosen sind nicht grün!' And because I didn't find a regex to simulate a str_split I optimized the first solution from adjwilli a bit: <?php $string = 'Weiße Rosen sind nicht grün!' $stop = mb_strlen( $string); $result = array(); for( $idx = 0; $idx < $stop; $idx++) { $result[] = mb_substr( $string, $idx, 1); } ?> Let me know, if someone found a regex to simulate a str_split with mb_split. http://php.net/manual/en/function.mb-split.php

« previous php.notes (#159035) next »