使用單次內建函式呼叫將 UTF-8 字串中的所有字元提取到陣列的(更簡單的)方法
<?php
$str = 'Ма-
руся';
print_r(preg_split('//u', $str, null, PREG_SPLIT_NO_EMPTY));
?>
輸出
陣列
(
[0] => М
[1] => а
[2] => -
[3] =>
[4] => р
[5] => у
[6] => с
[7] => я
)
(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
mb_split — 使用正規表示式分割多位元組字串
使用正規表示式 pattern
切割多位元組 string
並將結果以 陣列 形式返回。
注意:
預設情況下,此函式會使用 mb_regex_encoding() 指定的字元編碼作為字元編碼。
使用單次內建函式呼叫將 UTF-8 字串中的所有字元提取到陣列的(更簡單的)方法
<?php
$str = 'Ма-
руся';
print_r(preg_split('//u', $str, null, PREG_SPLIT_NO_EMPTY));
?>
輸出
陣列
(
[0] => М
[1] => а
[2] => -
[3] =>
[4] => р
[5] => у
[6] => с
[7] => я
)
與其他正規表示式函式(例如 preg_match)不同,$pattern 參數不使用 /pattern/ 分隔符號。
<?php
# 這樣可以運作。 /pattern/ 周圍不需要斜線
print_r( mb_split("\s", "hello world") );
Array (
[0] => hello
[1] => world
)
# 這樣不行:
print_r( mb_split("/\s/", "hello world") );
Array (
[0] => hello world
)
?>
我想大多數人都希望有一個簡單的方法可以將多位元組字串分解成個別的字元。以下是我用來達成此目的的函式。請將 UTF-8 更改為您選擇的編碼方法。
<?php
function mbStringToArray ($string) {
$strlen = mb_strlen($string);
while ($strlen) {
$array[] = mb_substr($string,0,1,"UTF-8");
$string = mb_substr($string,1,$strlen,"UTF-8");
$strlen = mb_strlen($string);
}
return $array;
}
?>
要使用「、」作為分隔符號來分割像「日、に、本、ほん、語、ご」這樣的字串,我使用了
$v = mb_split('、',"日、に、本、ほん、語、ご");
但卻無法運作。
解決方案是在此之前設定:
mb_regex_encoding('UTF-8');
mb_internal_encoding("UTF-8");
$v = mb_split('、',"日、に、本、ほん、語、ご");
現在可以正常運作了
陣列
(
[0] => 日
[1] => に
[2] => 本
[3] => ほん
[4] => 語
[5] => ご
)
補充 Sezer Yalcin 的訣竅。
這個函式可以將多位元組字串分割成一個字元陣列。類似於 str_split()。
<?php
函數 mb_str_split( $string ) {
# 在每個位置分割,而不是在開頭之後:^
# 並且不在結尾之前:$
返回 preg_split('/(?<!^)(?!$)/u', $string );
}
$string = '火車票';
$charlist = mb_str_split( $string );
print_r( $charlist );
?>
# 輸出
陣列
(
[0] => 火
[1] => 車
[2] => 票
)
我同意有些人可能想要一個 mb_explode('', $string);
這是我的解決方案
<?php
$string = 'Hallöle';
$array = array_map(函數($i) use ($string) {
返回 mb_substr($string, $i, 1);
}, range(0, mb_strlen($string) -1));
expect($array)->toEqual(['H', 'a', 'l', 'l', 'ö', 'l', 'e']);
?>
我們這裡談論的是多位元組(例如 UTF-8)字串,因此 preg_split 對於以下字串將會失敗
'Weiße Rosen sind nicht grün!'
因為我找不到一個 regex 來模擬 str_split,所以我稍微優化了 adjwilli 的第一個解決方案
<?php
$string = 'Weiße Rosen sind nicht grün!'
$stop = mb_strlen( $string);
$result = 陣列();
for( $idx = 0; $idx < $stop; $idx++)
{
$result[] = mb_substr( $string, $idx, 1);
}
?>
這是使用 adjwilli 函數的範例
<?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>';
?>
如果有人找到使用 mb_split 模擬 str_split 的正規表達式,請[透過我的個人電子郵件]告訴我。
另一種分割多位元組字串的方法
<?php
$s='әӘөүҗңһ';
// $temp_s = iconv('UTF-8', 'UTF-16', $s);
$temp_s=mb_convert_encoding($s,'UTF-16','UTF-8');
$temp_a=str_split($temp_s,4);
$temp_a_len=count($temp_a);
for($i=0;$i<$temp_a_len;$i++){
// $temp_a[$i] = iconv('UTF-16', 'UTF-8', $temp_a[$i]);
$temp_a[$i]=mb_convert_encoding($temp_a[$i],'UTF-8','UTF-16');
}
echo('<pre>');
print_r($temp_a);
echo('</pre>');
// 也可直接使用 UTF-16:
define('SLS',mb_convert_encoding('/','UTF-16'));
$temp_s=mb_convert_encoding($s,'UTF-16','UTF-8');
$temp_a=str_split($temp_s,4);
$temp_s=implode(SLS,$temp_a);
$temp_s=mb_convert_encoding($temp_s,'UTF-8','UTF-16');
echo($temp_s);
?>