Jump to content

Big5 String to array


victorw

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.