美文网首页
十七、PHP去除string中的bom

十七、PHP去除string中的bom

作者: yuzhan550 | 来源:发表于2017-11-21 22:24 被阅读36次
<?php
/**
 * 该文件,旨在为了去除php string里的bom
 * bom存在的可能性有:1个或2个,
 * 1个的位置可能是: 首部,尾部,中间
 * 2个可能的位置是首部+尾部
 * @return $res['val']  即为最终的结果
 * @param $handle_ok 0代表仍有bom  1代表bom清除完毕
 */
$contents =    '13512340680 ';    //  This character contains 'bom'  226  128  172
//$contents =    '13812340919';   //
//$contents =    '15812349681 ';  //
//$contents =    '13712346524';   //
//$contents =    's13512340680';  //

//  Step 1:  trim
$contents = trim($contents);

function RemoveBom($contents,$handle_ok=0){
    //  Step 2:   extract the length of this str,judge it if contains 有bom(process)
    $length = strlen($contents);
    $have_bom = 0;
    // The position of bom
    $bom_position = 0;
    for($i=0;$i<$length;$i++){
        $temp_single_element = substr($contents,$i,1);
        $is_num = ord($temp_single_element);
        if( $is_num >= 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

相关文章

  • 十七、PHP去除string中的bom

  • 批量去除BOM脚本PHP版

    今天部署一台新的服务器,部署好后发现验证码不能正确显示。检查了代码,确定没有问题,用Fiddle抓取了验证码的数据...

  • Java list 去重

    去除List中重复的 String 去除List中重复的对象 Person 对象: 根据name去重: 根据nam...

  • js中对字符串(String)去除空格

    js中对字符串(String)去除空格 str为要去除空格的字符串:去除所有空格:str = str.re...

  • PHP如何判断空

    PHP类型比较 PHP自动类型转换(弱语言) PHP去除空格 1、去除两边的空格trim($arr) 2、正则匹配...

  • PHP的exec()

    php中可以使用 exec() 函数调用外部函数。 语法: string exec ( string $comma...

  • smarty模板引擎

    一、模板引擎的工作原理 1、实现HTML代码和PHP代码简单分离,完全去除视图文件中的PHP标记 2、常用PHP模...

  • Twig之过滤页面的html元素

    一、去除字符串中的 HTML/PHP 标记 1、带HTML元素的效果图 2、过滤字符串中的 HTML/PHP 标记...

  • 字符串

    String的判断功能: String的获取功能 String的转换功能 String类的其他功能:替换功能去除字...

  • php chunk_split函数怎么用?

    chunk_split()函数在PHP中的一个内置函数,语法为string chunk_split($string...

网友评论

      本文标题:十七、PHP去除string中的bom

      本文链接:https://www.haomeiwen.com/subject/lregvxtx.html