美文网首页程序员PHPPHP开发
PHP常用函数整理(一)

PHP常用函数整理(一)

作者: 阿狸__小姑凉 | 来源:发表于2018-06-04 14:26 被阅读48次

    数学函数

    • abs(): 求绝对值

    • ceil():进一法取整

    • floor():舍去法取整

    • fmod():浮点数取余

      <?php
       $a = 5.6;
       $b = 2.1;
       $r = fmod($a, $b);
       echo $r; //1.4 因为 2.1*2+1.4=5.6
      ?>
      
    • pow():返回数的n次方

      <?php
       echo pow(2,4);  // 16
      ?>
      
    • round():浮点数四舍五入

      <?php
        echo round(1.323232, 3);  //1.32
      ?>
      
    • sqrt():求平方根

    • max():求最大值

    • min():求最小值

    • mt_rand():更好的随机数

    • rand():随机数

    • pi():获取圆周率值

    • trim():去除两边的空格
      rtrim():去除右边的空格
      chop():rtrim的别名
      ltrim():去除左边的空格

    • dirname():返回路径中的目录部分

    • str_pad():把字符串填充为指定的长度

      $str = "Hello World";
      echo str_pad($str,20,"."); //Hello World.........
      

      输入:要填充的字符串|新字符串的长度|供填充使用的字符串,默认是空白
      输出:完成后的字符串

    • str_repeat():重复使用指定的字符串

      echo str_repeat(".",13);//.............
      
    • str_split():把字符串分割到数组中

      print_r(str_split("Hello"));
      /* Array
          (
          [0] => H
          [1] => e
          [2] => l
          [3] => l
          [4] => o
      )*/
      

      输入:要分割的字符串|每个数组元素的长度,默认1
      输出:拆分后的字符串数组

    • strrev():反转字符串

      echo strrev("Hello World!"); // !dlroW olleH
      

      输出:目标字符串颠倒顺序后的字符串

    • wordwrap():按照指定长度对字符串进行折行处理

      $str = "An example on a long word is:
      Supercalifragulistic";
      echo wordwrap($str,15);
      /*An example on a
      long word is:
      Supercalifragulistic*/
      

      输入:目标字符串|最大宽数
      输出:折行后的新字符串

    • str_shuffle():随机的打乱字符串中所有字符

      echo str_shuffle("Hello World");//Wel ololHdr
      

      输入:目标字符串顺序
      输出:打乱后的字符串

    • parse_str():将字符串解析成变量

      parse_str("id=23&name=John%20Adams", $myArray);
      print_r($myArray);
      /*Array
      (
        [id] => 23
        [name] => John Adams
      )*/
      

      输入:要解析的字符串|存储变量的数组名称
      输出:返回数组

    • number_format():通过千位分组来格式化数字
      输入:要格式化的数字|规定多少个小数|规定用作小数点的字符串|规定用作千位分隔符的字符串

    大小与转换

    • strtolower():字符串转成小写
    • strtoupper():字符串转成大写
    • ucfirst():字符串首字母大写
    • unwords():字符串每个单词首字符转成大写

    html标签关联

    • htmlentities():把字符转为html实体

      $str = "John & 'Adams'";
      echo htmlentities($str, ENT_COMPAT); // John & 'Adams'
      
    • htmlspecialchars():预定义字符转成html编码

    • nl2br():\n转成 < br >标签

    • strip_tags():剥去html,xml 以及PHP标签

      echo strip_tags("Hello <b>world!</b>"); //Hello world!
      
    • addcslashes:在指定的字符前添加反斜杠转义字符串中的字符

      $str = "Hello, my name is John Adams.";
      echo $str."\n";//Hello, my name is John Adams.
      echo addcslashes($str,'m');//Hello, \my na\me is John Ada\ms.
      

      输入:目标字符串|指定的指定字符或者字符范围

    • stripcslashes():删除由addcslashes添加的反斜线

    • addslashes():指定预定义字符前添加反斜线

      $str = "Who's John Adams?";
      echo addslashes($str);//Who\'s John Adams?
      

      输出:把目标串中的'"\和null进行转义处理

    • stripslashes():删除由addslashes()添加的转义字符

    • quotemeta():在字符串中某些预定义字符前面添加反斜线

    • chr():从指定的ASCII值返回字符

    • ord():返回字符串第一个字符的ASCII值

    字符串比较

    • stricasecmp():不区分大小写的比较两个字符串
      输入:两个目标字符串
      输出:大1|等0|小-1
    • strcmp():区分大小写的比较两个字符串
    • strncmp():比较字符串的前n个字符,不区分大小写
    • strnatcmp():自然顺序法比较字符串长度,区分大小写
    • strnatcasecmp():自然顺序法比较字符串长度,不区分大小写

    字符串切割与拼接

    • chunk_split():将字符串分成小块
      调用:str chunk_split(str $body[,int $len[,str $end]])
      输入:$body目标字符串,$len长度,$str插入结束符
      输出:分割后的字符串
    • strtok():切开字符串
      调用:str strtok(str $str, str $token)
      目标字符串$str,以$token为标志切割返回切割后的字符串
    • explode():使用一个字符串为标志分割另一个字符串
      调用:array explode(str $sep,str $str[, int $limit])
      输入:$sep为分隔符,$str目标字符串,$limit返回数组最多包含元素数
      输出:字符串被分割后形成的数组
    • implode():同join,将数组的值用预定字符链接成字符串
    • substr():截取字符串
      调用:string substr(string $string, int $start [, int $length])

    字符串查找替换

    • str_replace():字符串替换操作,区分大小写
      调用mix str_replace(mix $search,mix $subject[, int $num]);
      输入:$search查找的字符串,$replace替换的字符串,$subject被查找字符串,&$num
      输出:返回替换后的结果

    • str_irplace():字符串替换操作,不区分大小写
      调用:mix str_ireplace(mix $search,mix $replace, mix $subject[, int &$count])
      输入:$search 查找的字符串,$replace替换的字符串, $subject被查找的字符串,&$num
      输出:返回替换后的结果

    • substr_count():统计一个字符串在另外一个字符串中出现的次数

    • substr_replace():替换字符串中某串为另一个字符串

      echo substr_replace('Hello world!', '1111', 3);//Hel1111
      

      调用:mixed substr_replace(mixed $string, string $replacement,int $start[,int $length])

    • similar_text():返回两个字符串相同字符的数量

      echo similar_text('textssss', 'test');//3
      

      调用:int similar_text(str $str1, str $str2);
      输入:两个比较的字符串
      输出:整型,相同字符数量

    • strrchr():返回一个字符串在另外一个字符串中最后一次出现位置开始到末尾的字符串
      调用:string strstr(string $haystack, mixed$needle)

    • stristr():返回一个字符串在另一个字符串中开始位置到结束的字符串,不区分大小写。
      调用:string stristr(string $haystack, mixed $needle[, bool $before_needle = false])

    • strtr():转换字符串中的某些字符

      echo strtr('hello world', 'hello', 'no');//nollo world
      

      调用:string strtr(string $str, string $from, string $to)

    • strpos():寻找字符串中某个字符最先出现的位置

      echo strpos('hello world', 'o');//4
      

      调用:int strpos(string $haystack, mixed $needle[, int $offset = 0])

    • stripos():寻找字符串某字符最先出现的位置,不区分大小写

      echo stripos('hello world', 'O');//4
      

      调用:int stripos(string $haystack,string $needle[,int $offset])

    • strrpos():寻找某字符串中某字符最后出现的位置

      echo strrpos('hello world', 'o');//7
      

      调用:int strrpos(string $haystack, string $needle[,int $offset])

    • strripos():寻找某字符串中某字符最后出现的位置,不区分大小写

      echo strripos("hello world", "O");//7
      调用:int strripos(string $haystack, string $needle[, int $offset])

    • strspn():返回字符串中首次符合mask的子字符串长度
      调用:int strspn(string $str1, string $str2[,int $start[,int $length]])

    • strcspn():返回字符串中不符合mask的字符串的长度
      调用:int strcspn(string $str1, string $str2[,int $start[, int $length]])
      输入:$str1被查询,$str2查询字符串,$start开始查询的字符,$length是查询长度
      输出:返回从开始到第几个字符串

    字符串统计

    • str_word_count():统计字符串含有的单词数

      echo str_word_count('hello world, do you like me');//6
      

      调用: mix str_word_count(str $str,[])
      输入:目标字符串
      输出:统计出的数量

    • strlen():统计字符串长度int
      输入:目标字符串
      输出:整型长度

    • count_chars():统计字符串中所有字母出现次数(0.255)
      调用: mixed count_chars ( string $string [, int $mode ] )

    字符串编码

    • md5():字符串md5加密

      $str = 'hello world';
      echo md5($str);//5eb63bbbe01eeed093cb22bb8f5acdc3
      

    相关文章

      网友评论

      • 酷卜伊:round方法第二个参数表示的是小数点后的位数!
        阿狸__小姑凉:已经更改了,谢谢
      • 东流_:谢谢楼主 我是初学者 看到这一篇对我的帮助很大

      本文标题:PHP常用函数整理(一)

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