美文网首页
php字符串函数(部分)

php字符串函数(部分)

作者: xiaolin_188 | 来源:发表于2016-11-14 18:04 被阅读0次

    1. echo,print

    都可输出字符串,但echo比print快

     echo 'hello world';
    

    2. printf,sprintf

    都是格式化(format)输出,但printf会直接输出格式化的内容,而sprintf是返回格式化的内容,通过echo输出。

    //%s字符串,%u大于等于0的十进制
    printf('我要在%s买%u套房', '北京', 1); //我要在北京买1套房
    

    3. explode,implode(join)

    前者:字符串拆分成数组
    后者:数组转换成字符串,implode == join

    print_r(explode(',', '1,2,3,4,5,6'));
    /*
    Array
    (
        [0] => 1
        [1] => 2
        [2] => 3
        [3] => 4
        [4] => 5
        [5] => 6
    )
    */
    
    echo implode(',', array(1,2,3,4,5,6)); //1,2,3,4,5,6
    

    4. htmlspecialchars,htmlspecialchars_decode

    前者:将字符串中的HTML实体转义
    后者:将转义的字符转换成HTML实体

    echo htmlspecialchars('<"&>'); //<"&>
    echo htmlspecialchars_decode('<"&>'); //<"&>
    

    5. ltrim,rtrim,trim

    删除字符串左,右,两边的空白字符(空格、\n、\t、\r、\0、\x0B)或其他预定义字符,区分大小写

    注意:上面说的是字符,也就不是整个字符串,只要前后有其中一个字符都丢弃,直到遇到其他字符为止

    echo trim('hhhdddhaehhhccccceeeedddddfddee!!!ee', 'hed!'); //aehhhccccceeeedddddf
    

    6. md5,md5_file,sha1,sha1_file

    md5、sha1字符串散列,可以存储密码,参数完整性校验等
    md5_file、sha1_file文件散列,可以用于检测文件内容是否更改

    简单介绍区别:
    MD5与SHA1都是Hash算法,MD5输出是128位的,SHA1输出是160位的,MD5比SHA1快,SHA1比MD5强度高。

    7. str_replace,str_ireplace

    字符串或者数组值替换。str_replace区分大小写,str_ireplace不区分大小写

    8. strlen,mb_substr,substr,mb_substr

    echo substr('Hello world', 10). '<br>'; //d
    echo substr('Hello world', 1). '<br>'; //ello world
    echo substr('Hello world', 3). '<br>'; //lo world
    echo substr('Hello world', 7). '<br>'; //orld
    
    echo substr('Hello world', -1). '<br>'; //d
    echo substr('Hello world', -10). '<br>'; //ello world
    echo substr('Hello world', -8). '<br>'; //lo world
    echo substr('Hello world', -4). '<br>'; //orld
    

    9. strstr(strchr),stristr,strrchr,strpos,strrpos,stripos,strripos

    strstr() -搜索字符串在另一字符串中的第一次出现,并返回字符串的剩余部分(区分大小写)
    stristr() -搜索字符串在另一字符串中的第一次出现,并返回字符串的剩余部分(不区分大小写)
    strrchr() -搜索字符串在另一字符串中的最后一次出现,并返回字符串的剩余部分(区分大小写)

    strpos() -找字符串在另一字符串中第一次出现的位置(区分大小写)
    stripos() - 查找字符串在另一字符串中第一次出现的位置(不区分大小写)
    strrpos() - 查找字符串在另一字符串中最后一次出现的位置(区分大小写)
    strripos() - 查找字符串在另一字符串中最后一次出现的位置(不区分大小写)

    10. strtolower,strtoupper

    前者:字符串转为小写
    后者:字符串转为大写

    11. strrev

    字符串反转

    相关文章

      网友评论

          本文标题:php字符串函数(部分)

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