Home > 类库下载 > PHP类库 > PHP convert a string to array

PHP convert a string to array

高洛峰
Release: 2016-10-20 16:35:11
Original
2004 people have browsed it

PHP converts a string into an array, supports Chinese

/**
 * 将一个字符串转换成数组,支持中文
 * @param string    $string   待转换成数组的字符串
 * @return string   转换后的数组
 */
function strToArray($string) {
    $strlen = mb_strlen($string);
    while ($strlen) {
        $array[] = mb_substr($string, 0, 1, "utf8");
        $string = mb_substr($string, 1, $strlen, "utf8");
        $strlen = mb_strlen($string);
    }
    return $array;
}
Copy after login


Usage:

$string = '这里就是要转换成数组的字符串,www.liqingbo.cn';
$arr = strToArray($string);
print_r($arr);
Copy after login

Output:

Array
(
    [0] => 这
    [1] => 里
    [2] => 就
    [3] => 是
    [4] => 要
    [5] => 转
    [6] => 换
    [7] => 成
    [8] => 数
    [9] => 组
    [10] => 的
    [11] => 字
    [12] => 符
    [13] => 串
    [14] => ,
    [15] => w
    [16] => w
    [17] => w
    [18] => .
    [19] => l
    [20] => i
    [21] => q
    [22] => i
    [23] => n
    [24] => g
    [25] => b
    [26] => o
    [27] => .
    [28] => c
    [29] => n
)
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template