十七、PHPはstring中のbomを取り除く

2582 ワード

= 48 && $is_num <= 57 ){
            //  no code in this line
        }else{
            $have_bom = 1;
            $bom_position = $i;
            break;
        }
    }

    // Step 3:  Judge if have bom(result), if have ,then remove it :the position of bom: a. header b. footer  c. middle
    if($have_bom==1){
        // Judge header
        if($bom_position==0){
            //echo 'header';die;
            $res['handle_ok'] = 0 ;
            $res['val'] = substr($contents,3); // If no the third param, its default value is until to the tail of this string
            return $res ;
        // Judge footer
        }else if( $bom_position == ($length-3) ){
            //echo 'footer';die;
            $res['handle_ok'] = 0 ;
            $res['val'] = substr($contents,0,$length-3 );
            return $res ;
        // Judge middle
        }else {
            //echo 'middle';die;
            $starter = substr($contents,0,$bom_position );
            $ender   = substr($contents,$bom_position+3 );
            $res['handle_ok'] = 0 ;
            $res['val'] = $starter . $ender ;

            return $res ;
        }

    }else{
        // No bom
        $res['handle_ok'] = 1;
        $res['val'] = $contents;
        return $res ;
    }

}
$res = RemoveBom($contents);    // Remove the first one
$res = RemoveBom($res['val']);  // Remove the second one
$res = RemoveBom($res['val']);  // make $handle_ok become one
if($res['handle_ok']==1){
    var_dump($res);
    die;
}else{
    echo 'Still have bom';
    die;
}

// Judge if it is phone number with Regular Expression