PHP常用函數(shù)(一)

數(shù)學(xué)函數(shù)

  • abs(): 求絕對值

  • ceil():進一法取整

  • floor():舍去法取整

  • fmod():浮點數(shù)取余

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

    <?php
     echo pow(2,4);  // 16
    ?>
    
  • round():浮點數(shù)四舍五入

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

  • max():求最大值

  • min():求最小值

  • mt_rand():更好的隨機數(shù)

  • rand():隨機數(shù)

  • pi():獲取圓周率值

  • trim():去除兩邊的空格
    rtrim():去除右邊的空格
    chop():rtrim的別名
    ltrim():去除左邊的空格

  • dirname():返回路徑中的目錄部分

  • str_pad():把字符串填充為指定的長度

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

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

  • str_repeat():重復(fù)使用指定的字符串

    echo str_repeat(".",13);//.............
    
  • str_split():把字符串分割到數(shù)組中

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

    輸入:要分割的字符串|每個數(shù)組元素的長度,默認1
    輸出:拆分后的字符串?dāng)?shù)組

  • strrev():反轉(zhuǎn)字符串

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

    輸出:目標(biāo)字符串顛倒順序后的字符串

  • wordwrap():按照指定長度對字符串進行折行處理

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

    輸入:目標(biāo)字符串|最大寬數(shù)
    輸出:折行后的新字符串

  • str_shuffle():隨機的打亂字符串中所有字符

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

    輸入:目標(biāo)字符串順序
    輸出:打亂后的字符串

  • parse_str():將字符串解析成變量

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

    輸入:要解析的字符串|存儲變量的數(shù)組名稱
    輸出:返回數(shù)組

  • number_format():通過千位分組來格式化數(shù)字
    輸入:要格式化的數(shù)字|規(guī)定多少個小數(shù)|規(guī)定用作小數(shù)點的字符串|規(guī)定用作千位分隔符的字符串

大小與轉(zhuǎn)換

  • strtolower():字符串轉(zhuǎn)成小寫
  • strtoupper():字符串轉(zhuǎn)成大寫
  • ucfirst():字符串首字母大寫
  • unwords():字符串每個單詞首字符轉(zhuǎn)成大寫

html標(biāo)簽關(guān)聯(lián)

  • htmlentities():把字符轉(zhuǎn)為html實體

    $str = "John & 'Adams'";
    echo htmlentities($str, ENT_COMPAT); // John & 'Adams'
    
  • htmlspecialchars():預(yù)定義字符轉(zhuǎn)成html編碼

  • nl2br():\n轉(zhuǎn)成 < br >標(biāo)簽

  • strip_tags():剝?nèi)tml,xml 以及PHP標(biāo)簽

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

    $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.
    

    輸入:目標(biāo)字符串|指定的指定字符或者字符范圍

  • stripcslashes():刪除由addcslashes添加的反斜線

  • addslashes():指定預(yù)定義字符前添加反斜線

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

    輸出:把目標(biāo)串中的'"\和null進行轉(zhuǎn)義處理

  • stripslashes():刪除由addslashes()添加的轉(zhuǎn)義字符

  • quotemeta():在字符串中某些預(yù)定義字符前面添加反斜線

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

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

字符串比較

  • stricasecmp():不區(qū)分大小寫的比較兩個字符串
    輸入:兩個目標(biāo)字符串
    輸出:大1|等0|小-1
  • strcmp():區(qū)分大小寫的比較兩個字符串
  • strncmp():比較字符串的前n個字符,不區(qū)分大小寫
  • strnatcmp():自然順序法比較字符串長度,區(qū)分大小寫
  • strnatcasecmp():自然順序法比較字符串長度,不區(qū)分大小寫

字符串切割與拼接

  • chunk_split():將字符串分成小塊
    調(diào)用:str chunk_split(str body[,intlen[,str end]]) 輸入:body目標(biāo)字符串,len長度,str插入結(jié)束符
    輸出:分割后的字符串
  • strtok():切開字符串
    調(diào)用:str strtok(str str, strtoken)
    目標(biāo)字符串str,以token為標(biāo)志切割返回切割后的字符串
  • explode():使用一個字符串為標(biāo)志分割另一個字符串
    調(diào)用:array explode(str sep,strstr[, int limit]) 輸入:sep為分隔符,str目標(biāo)字符串,limit返回數(shù)組最多包含元素數(shù)
    輸出:字符串被分割后形成的數(shù)組
  • implode():同join,將數(shù)組的值用預(yù)定字符鏈接成字符串
  • substr():截取字符串
    調(diào)用:string substr(string string, intstart [, int $length])

字符串查找替換

  • str_replace():字符串替換操作,區(qū)分大小寫
    調(diào)用mix str_replace(mix search,mixsubject[, int num]); 輸入:search查找的字符串,replace替換的字符串,subject被查找字符串,&$num
    輸出:返回替換后的結(jié)果

  • str_irplace():字符串替換操作,不區(qū)分大小寫
    調(diào)用:mix str_ireplace(mix search,mixreplace, mix subject[, int &count])
    輸入:search 查找的字符串,replace替換的字符串, subject被查找的字符串,&num
    輸出:返回替換后的結(jié)果

  • substr_count():統(tǒng)計一個字符串在另外一個字符串中出現(xiàn)的次數(shù)

  • substr_replace():替換字符串中某串為另一個字符串

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

    調(diào)用:mixed substr_replace(mixed string, stringreplacement,int start[,intlength])

  • similar_text():返回兩個字符串相同字符的數(shù)量

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

    調(diào)用:int similar_text(str str1, strstr2);
    輸入:兩個比較的字符串
    輸出:整型,相同字符數(shù)量

  • strrchr():返回一個字符串在另外一個字符串中最后一次出現(xiàn)位置開始到末尾的字符串
    調(diào)用:string strstr(string haystack, mixedneedle)

  • stristr():返回一個字符串在另一個字符串中開始位置到結(jié)束的字符串,不區(qū)分大小寫。
    調(diào)用:string stristr(string haystack, mixedneedle[, bool $before_needle = false])

  • strtr():轉(zhuǎn)換字符串中的某些字符

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

    調(diào)用:string strtr(string str, stringfrom, string $to)

  • strpos():尋找字符串中某個字符最先出現(xiàn)的位置

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

    調(diào)用:int strpos(string haystack, mixedneedle[, int $offset = 0])

  • stripos():尋找字符串某字符最先出現(xiàn)的位置,不區(qū)分大小寫

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

    調(diào)用:int stripos(string haystack,stringneedle[,int $offset])

  • strrpos():尋找某字符串中某字符最后出現(xiàn)的位置

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

    調(diào)用:int strrpos(string haystack, stringneedle[,int $offset])

  • strripos():尋找某字符串中某字符最后出現(xiàn)的位置,不區(qū)分大小寫

    echo strripos("hello world", "O");//7
    調(diào)用:int strripos(string haystack, stringneedle[, int $offset])

  • strspn():返回字符串中首次符合mask的子字符串長度
    調(diào)用:int strspn(string str1, stringstr2[,int start[,intlength]])

  • strcspn():返回字符串中不符合mask的字符串的長度
    調(diào)用:int strcspn(string str1, stringstr2[,int start[, intlength]])
    輸入:str1被查詢,str2查詢字符串,start開始查詢的字符,length是查詢長度
    輸出:返回從開始到第幾個字符串

字符串統(tǒng)計

  • str_word_count():統(tǒng)計字符串含有的單詞數(shù)

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

    調(diào)用: mix str_word_count(str $str,[])
    輸入:目標(biāo)字符串
    輸出:統(tǒng)計出的數(shù)量

  • strlen():統(tǒng)計字符串長度int
    輸入:目標(biāo)字符串
    輸出:整型長度

  • count_chars():統(tǒng)計字符串中所有字母出現(xiàn)次數(shù)(0.255)
    調(diào)用: mixed count_chars ( string string [, intmode ] )

字符串編碼

  • md5():字符串md5加密

    $str = 'hello world';
    echo md5($str);//5eb63bbbe01eeed093cb22bb8f5acdc3
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容