victorw Posted October 28, 2008 Share Posted October 28, 2008 Hi , I want to split a string that contain big5 chinese character to array. for example $string = "呀丫啞" to > Array /( /[0] => 呀 [1] => 丫 [2] => 啞 ) I know that php have a preg-split function but it doesn't work well for big5 character coding. please help ... Thanks in advance . <?php //http://au.php.net/manual/en/function.preg-split.php $str = 'string'; $chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); print_r($chars); ?> Array /( /[0] => s [1] => t [2] => r [3] => i [4] => n [5] => g ) Link to comment https://forums.phpfreaks.com/topic/130395-big5-string-to-array/ Share on other sites More sharing options...
thebadbad Posted October 28, 2008 Share Posted October 28, 2008 You can just access each char in the string like it's an array: <?php $string = 'abc'; echo $string[0]; //a ?> But of course, if you need it to be an array, you can try this if anything else fails: <?php $array = array(); for ($i = 0; $i < strlen($string); $i++) { $array[] = $string[$i]; } ?> Link to comment https://forums.phpfreaks.com/topic/130395-big5-string-to-array/#findComment-676375 Share on other sites More sharing options...
thebadbad Posted October 28, 2008 Share Posted October 28, 2008 Actually, I can't get it to work either. Must have something to do with the character set used. Link to comment https://forums.phpfreaks.com/topic/130395-big5-string-to-array/#findComment-676377 Share on other sites More sharing options...
thebadbad Posted October 28, 2008 Share Posted October 28, 2008 I got it working using multibyte functions: $string = '呀丫啞'; <?php $array = array(); for ($i = 0; $i < mb_strlen($string, 'utf-8'); $i++) { $array[] = mb_substr($string, $i, 1, 'utf-8'); } echo '<pre>', print_r($array, true), '</pre>'; ?> Be sure to save the file in UTF-8 encoding. Edit: I had to put $string outside of the code block for it to show properly here. Link to comment https://forums.phpfreaks.com/topic/130395-big5-string-to-array/#findComment-676382 Share on other sites More sharing options...
victorw Posted October 28, 2008 Author Share Posted October 28, 2008 I got it working using multibyte functions: $string = '呀丫啞'; <?php $array = array(); for ($i = 0; $i < mb_strlen($string, 'utf-8'); $i++) { $array[] = mb_substr($string, $i, 1, 'utf-8'); } echo '<pre>', print_r($array, true), '</pre>'; ?> Be sure to save the file in UTF-8 encoding. Edit: I had to put $string outside of the code block for it to show properly here. Thanks, it is working Link to comment https://forums.phpfreaks.com/topic/130395-big5-string-to-array/#findComment-676421 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.