MENU

PHP-分割指定字符串为数组(支持空格、逗号(中英文)、回车、斜杠分割)

March 3, 2023 • PHP

if (!function_exists('str_split_to_array')) {
    function str_split_to_array($strs)
    {
        $result = [];
        $array = [];
        $strs = str_replace(',', ',', $strs); //可以添加多个分割方法
        $strs = str_replace(' ', ',', $strs);
        $strs = str_replace('/', ',', $strs);
        $strs = str_replace("\r", ',', $strs);
        $strs = str_replace("\n", ',', $strs);

        $array = explode(',', $strs);
        foreach ($array as $key => $value) {
            if (($value = trim($value)) != '') {
                $result[] = $value;
            }
        }
        return $result;
    }
}