• php中字符串常用方法


    1.ltrim();删去字符串中的前导空格;

    $string1 = " zhangsan";
    $result = ltrim($string1);
    var_dump($result);
    

    2.rtrim();用来删去字符串中的后缀空格;

    $string2 = " zhang san ";
    $result = rtrim($string2);
    var_dump($result);
    

    3.trim();截取字符串中首尾空格;

    $string3 = " zhang san ";
    $result = trim($string3);
    var_dump($result);
    

    4.str_pad();按需求对字符串进行填充;需添加四项参数,第一项为被填充的字符串,第二项为填充后字符串长度,第三项为填充的内容,第四项为填充位置(STR_PAD_LEFT,STR_PAD_RIGHT,STR_PAD_BOTH);

    $string4 = "zhangsan";
    $result = str_pad($string4,12,"-=",STR_PAD_LEFT);
    var_dump($result);
    

    5.strtolower();将字符串全部变成小写;

    $string5 = "ZHANGSAN";
    $result = strtolower($string5);
    var_dump($result);
    

    6.strtoupper();将字符串全部变成大写;

    $string6 = "zhangsan";
    $result = strtoupper($string6);
    var_dump($result);
    

    7.ucfirst();将字符串第一个字符改大写;

    $string7 = "zhangsan";
    $result = ucfirst($string7);
    var_dump($result);
    

    8.ucwords();将字符串给个字第一个字符改大写;

    $string8 = "zhang san";
    $result = ucwords($string8);
    var_dump($result);
    

    9.nl2br();将字符串中 转成HTML换行符<br/>;

    $string9 = "zhang
    san";
    $result = nl2br($string9);
    var_dump($string8);
    var_dump($result);
    

    10.strip_tags();删除HTML标签函数;

    $string10 = "<P>zhangsan</P>";
    $result = strip_tags($string10);
    var_dump($string10);
    var_dump($result);
    

    11.strrev();将字符串前后颠倒;

    $string11 = "zhangsan";
    $result = strrev($string11);
    var_dump($string11);
    var_dump($result);
    

    12.strlen();取得字符串长度;

    $string12 = "zhangsan";
    $result = strlen($string12);
    var_dump($result);
    

    13.md();加密函数;

    $string13 = "zhangsan";
    $result = md5($string13);
    var_dump($result);
    

    14.strcmp();按字节比较字符串是否相同,返回结果为前面字符串长度减去后面字符串长度

    $string14 = "zhangsa";
    $string15 = "zhangsan";
    $result = strcmp($string14,$string15);
    echo $result;
    

    15.strncmp();比较指定长度的字符串;

    $string16 = "zhangsa";
    $string17 = "zanngsan";
    $result = strncmp($string16,$string17,2);
    echo $result;
    

    16.strcasecmp();不区分大小写的全串比较;

    $string18 = "Zhangsan";
    $string19 = "zhangsan";
    $result = strcasecmp($string18,$string19);
    echo $result;
    

    17.strnatcmp();按自然排序法时字符串的比较;

    $string20 = "21zhangsan";
    $string21 = "12zhangsan";
    $result = strnatcmp($string20,$string21);
    echo $result;
    

    18.similar_text();字符串的模糊比较,返回相似字符串的个数;

    $string22 = "zangsan";
    $string23 = "zhangsan";
    $result = similar_text($string22,$string23,$percent);
    echo $percent;
    

      

  • 相关阅读:
    CopyOnWriteArrayList分析
    java锁和同步
    线程池原理
    Hadoop1的安装
    Hadoop2的HA安装(high availability):JournalNode+ zookeeper
    Hadoop2的HA安装(high availability):nfs+zookeeper
    Hadoop2的FN安装(federated namespace)
    Redis 基础知识
    mycat
    GitFlow及项目工作流程
  • 原文地址:https://www.cnblogs.com/-xin/p/6582584.html
Copyright © 2020-2023  润新知