1. 程式人生 > 其它 >字串中多個空格無法用自帶函式(trim)去除

字串中多個空格無法用自帶函式(trim)去除

技術標籤:php去空格trimphp

在網上查了好多給出的方法,都沒解決

暫時定了個方法 批量處理

function trimSpace($text){
    $t = str_split($text);
    $pre = '';
    if(!empty($t)){
        $i = 0;
        $new = [];
        foreach ($t as $v){
            if($i!=0){
                $pre = ord($t[$i-1]);
            }
            $current = ord($v);
            $i++;
            if($current==$pre && $current==32){
                continue;
            }
            $new[] = $v;
        }
        $text = " ".join($new);
    }

    return $text;


}