PHP常用字串函數小結,php字串小結

來源:互聯網
上載者:User

PHP常用字串函數小結,php字串小結
一、判斷類型的函數

is_bool()       //判斷是否為布爾型is_float()      //判斷是否為浮點型is_real()       //同上is_int()        //判斷是否為整型is_integer()    //同上is_string()     //判斷是否為字串is_object()     //判斷是否為對象is_array()      //判斷是否為數組is_null()       //判斷是否為nullis_file()       //判斷是否為檔案is_dir()        //判斷是否為目錄is_numeric()    //判斷是否為數字is_nan()        //判斷不是數字is_resource()   //判斷是否為資源類型is_a($obj,$classname) //判斷對象是否為類的執行個體                      //可用 if($obj instanceof Classname)

二、擷取子串位置

strpos($hs,$nd [,int $offset = 0 ]) //返回nd 在 hs 中首次出現的數字位置。 stripos($hs,$nd [,int $offset = 0 ]) //返回nd 在 hs 中首次出現的數字位置, 不區分大小寫。strrpos($hs,$nd [,int $offset = 0 ]) //返回nd 在 hs 中最後一次出現的數字位置。strripos($hs,$nd [,int $offset = 0 ]) //返回nd 在 hs 中最後一次出現的數字位置,不區分大小寫。 

三、擷取子串

substr($str,$start [,$length]); //擷取子串substr_compare($main_str,$str,$offset[,$length]); //子串比較 從offset處開始比較substr_count($hs,$nd [,$offset=0 [,$length]]); //擷取子串nd在hs中出現的次數substr_replace($string,$replacement,$start [,$length]); //字串子串替換                               //用$replacement替換掉$string從start開始長度為length的子串strstr($hys,$nd [,bool $before_needle = false ]);//返回$nd在$hys 第一次出現的地方開始到字串結束 為止的字串               //第三個參數如果為true 則返回$nd 之前的字串stristr($hys,$nd [,bool $before_needle = false ]); //同上,忽略大小寫版本strrchr($hys,$nd); //返回$nd在$hys最後一次出現的地方開始到字串結束 為止的字串               //一般和 substr(strrchr($hys,$nd),strlen($nd)); 合用strpbrk($haystack,$char_list);//從$char_list中依次匹配單個字元在$haystack中第一次出現的地方                        //到字串結束的地方 的整個字串strtok($str,$token); //第一次使用 將字串按分隔字元$token進行分割strtok($token);      //第二次使用eg.$string = "This is\tan example\nstring";/* 使用定位字元和分行符號作為分界符 */$tok = strtok($string, " \n\t");while ($tok !== false) {echo "Word=$tok<br />";$tok = strtok(" \n\t");}

四、字串str_ 型函數

str_getcsv($str); //將csv檔案字串轉換成一個數組str_replace($search,$replace,$subject [,&$count]);//搜尋並替換字串           //第四個參數被指定的話,將會賦值給他替換的次數str_ireplace($search,$replace,$subject [,&$count]);//搜尋並替換字串           //第四個參數被指定的話,將會賦值給他替換的次數 忽略大小寫str_shuffle(string $str);//隨機打亂字串str_split($str [,$len=1]);//將字串轉換成一個數組                         //,每個數組單元的長度為$len

五、字串長度

strlen($str);  //字串長度

六、翻轉字串

strrev(string $string);// 翻轉字串

七、mb_類型字串函數

mb_類型字串與上述字串函數基本一樣,
只是加多一個可選的字元編碼參數,用法同上
這裡列出一些其他有用函數

1、檢測字串的字元編碼

$encode = mb_detect_encoding($lines, array("ASCII","UTF-8","GB2312","GBK","BIG5"));if($encode != "UTF-8"){    $lines = iconv($encode,"UTF-8", $lines);}

八、字串的相關操作1、轉換字串類型

strval($str); //轉換成字串類型floatval($str);//轉換成浮點型intval($str); //轉換成整型

2、大小寫轉換

strtolower($str); //全部轉換成小寫strtoupper($str); //全部轉換成大寫

3、字串轉時間戳記

strtotime($str); //時間格式的字串轉換成整型時間戳記           //注意設定時區 否則會有 8小時誤差

4、去除HTML 和 PHP 標記

strip_tags($str [,$tags]);//去除不含$tags裡標籤外的所有標籤

5、ascii轉數字 數字轉ascii

chr(int $ascii); //數字轉換成asciiord(string $str); //返回$str第一個字元的ascii值

6、json的編碼與解碼

json_encode($obj/$arr/$str...);//編碼成json 格式的字串json_decode($jsonstr [,$assoc=true]); //解碼成對象                         //當$assoc=true 時 返回數組 而非對象

7、換行轉<br/>

nl2br($str); //字串 $str 所有新行之前插入'<br/>'

8、數組轉字串,字串轉數組

implode($arr,$glue);//將一維數群組轉換為字串explode();//字串轉換為數組

9、千位分割格式化

string number_format ( float $number [, int $decimals = 0 ] )string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' ) @param   $number 你要格式化的數字           $decimals 要保留的小數位元           $dec_point 指定小數點顯示的字元           $thousands_sep 指定千位分隔字元顯示的字元 

10、去空格

trim(string $str [,string $charlist ]); //去左右字元ltrim(string $str [,string $charlist ]); //去左字元rtrim(string $str [,string $charlist ]); //去右字元

該函數刪除 str 末端的空白字元並返回。

不使用第二個參數, rtrim() 僅刪除以下字元:
• " " (ASCII 32 (0x20)),普通空白符。  
• "\t" (ASCII 9 (0x09)),定位字元。  
• "\n" (ASCII 10 (0x0A)),分行符號。  
• "\r" (ASCII 13 (0x0D)),斷行符號符。  
• "\0" (ASCII 0 (0x00)),NUL 空位元組符。  
• "\x0B" (ASCII 11 (0x0B)),垂直定位字元。
過濾字元也可由 charlist 參數指定。一般要列出所有希望過濾的字元,
也可以使用 ".." 列出一個字元範圍

11、轉換字串編碼函數

iconv($in_charset, $out_charset, $str);$in_charset輸入字元集$out_charset輸出字元集

12、字串加密函數

sha1($str); md5($str);

13、字串轉義與反轉義函數

addcslashes(string $str , string $charlist);//逸出字元串中的特殊字元               //eg. addcslashes($str,"\0..\37!@\177..\377");   //轉義ascii 中0-37、177-377中不含@符號的字元stripcslashes($str) — 反轉義addcslashes()函數轉義處理過的字串  返回反轉義後的字串。可識別類似 C 語言的 \n,\r,... 八進位以及十六進位的描述

14、按格式返回資料

sprintf — 按照要求對資料進行返回,但是不輸出可表示類型如下:string s integer d, u, c, o, x, X, b  double g, G, e, E, f, F  eg.$num = 5;$location = 'tree';$format = 'There are %d monkeys in the %s';echo sprintf($format, $num, $location);





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.