# php常用字符串函數
函數名| 描述 |實例
---|---|---
trim() |刪除字符串兩端的空格或其他預定義字符| "$str = ""\r\nHello World!\r\n""; echo trim($str);
rtrim() |刪除字符串右邊的空格或其他預定義字符 |"$str = ""Hello World!\n\n""; echo rtrim($str);"
chop() |rtrim()的別名 同上|
ltrim() |刪除字符串左邊的空格或其他預定義字符| "$str = ""\r\nHello World!""; echo ltrim($str);"
dirname() |回路徑中的目錄部分(我們把它歸在了字符串函數里了)| echo dirname("c:/testweb/home.php");
str_pad() |把字符串填充為指定的長度| $str = "Hello World"; echo str_pad($str,20,""."");
str_repeat()| 重復使用指定字符串| echo str_repeat(".",13);
str_split() |把字符串分割到數組中| print_r(str_split("Hello"));
strrev() |反轉字符串 |echo strrev("Hello World!");
wordwrap() |按照指定長度對字符串進行折行處理 |"$str = ""An example on a long word is: Supercalifragulistic""; echo wordwrap($str,15);"
str_shuffle()| 隨機地打亂字符串中所有字符| echo str_shuffle("Hello World");
parse_str() |將字符串解析成變量 |"parse_str(""id=23&name=John%20Adams"",$myArray); print_r($myArray);"
number_format()| 通過千位分組來格式化數字 |"echo number_format(""1000000""); echo number_format(""1000000"",2); echo number_format(""1000000"",2,"","",""."");"
strtolower()| 字符串轉為小寫 |echo strtolower("Hello WORLD!");
strtoupper()| 字符串轉為大寫| echo strtoupper("Hello WORLD!");
ucfirst() |字符串首字母大寫| echo ucfirst("hello world");
ucwords() |字符串每個單詞首字符轉為大寫| echo ucwords("hello world");
htmlentities()| 把字符轉為HTML實體| $str = ""John & 'Adams'""; echo htmlentities($str, ENT_COMPAT);
htmlspecialchars()| 預定義字符轉html編碼
nl2br() |\n轉義為標簽 | echo nl2br("One line.\nAnother line.");
strip_tags() | 剝去 HTML、XML 以及 PHP 的標簽 |echo strip_tags("Hello world!");
addcslashes() |在指定的字符前添加反斜線轉義字符串中字符 |$str = ""Hello, my name is John Adams." echo $str; echo addcslashes($str,'m');"
stripcslashes() |刪除由addcslashes()添加的反斜線 |echo stripcslashes("Hello, \my na\me is Kai Ji\m.");
addslashes() |指定預定義字符前添加反斜線 |$str = "Who's John Adams?";echo addslashes($str);
stripslashes() |刪除由addslashes()添加的轉義字符| echo stripslashes("Who's John Adams?");
quotemeta() |在字符串中某些預定義的字符前添加反斜線| $str = ""Hello world. (can you hear me?)""; echo quotemeta($str);
chr() |從指定的 ASCII 值返回字符| echo chr(052);
ord() |返回字符串第一個字符的 ASCII值| echo ord("hello");
strcasecmp() |不區分大小寫比較兩字符串| echo strcasecmp("Hello world!","HELLO WORLD!");
strcmp() |區分大小寫比較兩字符串 |
strncmp() |比較字符串前n個字符,區分大小寫 |
strncasecmp() |比較字符串前n個字符,不區分大小寫 |int strncasecmp ( string $str1 , string $str2 , int $len )
strnatcmp() | 自然順序法比較字符串長度,區分大小寫 |int strnatcmp ( string $str1 , string $str2 )
strnatcasecmp() |自然順序法比較字符串長度,不區分大小寫| int strnatcasecmp ( string $str1 , string $str2 )
chunk_split() |將字符串分成小塊 |str chunk_split(str $body[,int $len[,str $end]])
strtok() |切開字符串 |str strtok(str $str,str $token)
explode() |使用一個字符串為標志分割另一個字符串 |array explode(str $sep,str $str[,int $limit])
implode() |同join,將數組值用預訂字符連接成字符串| string implode ( string $glue , array $pieces )
substr() |截取字符串 |string substr ( string $string , int $start [, int $length ] )
str_replace() | 字符串替換操作,區分大小寫 |mix str_replace(mix $search,,mix $replace,mix $subject[,int &$num])
str_ireplace() |字符串替換操作,不區分大小寫 |mix str_ireplace ( mix $search , mix $replace , mix $subject [, int &$count ] )
substr_count() |統計一個字符串,在另一個字符串中出現次數 |int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )
substr_replace() | 替換字符串中某串為另一個字符串 |mixed substr_replace ( mixed $string , string $replacement , int $start [, int $length ] )
similar_text() | 返回兩字符串相同字符的數量 |int similar_text(str $str1,str $str2)
strchr() | 返回一個字符串在另一個字符串中開始位置到結束的字符串 |string strstr ( string $str, string $needle , bool $before_needle )
strrchr() | 返回一個字符串在另一個字符串中最后一次出現位置開始到末尾的字符串 |string strrchr ( string $haystack , mixed $needle )
stristr() | 返回一個字符串在另一個字符串中開始位置到結束的字符串,不區分大小寫 |string stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
strtr() | 轉換字符串中的某些字符 |string strtr ( string $str , string $from , string $to )
strpos() | 尋找字符串中某字符最先出現的位置 |int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
stripos() | 尋找字符串中某字符最先出現的位置,不區分大小寫| int stripos ( string $haystack , string $needle [, int $offset ] )
strrpos() | 尋找某字符串中某字符最后出現的位置| int strrpos ( string $haystack , string $needle [, int $offset = 0 ] )
strripos() | 尋找某字符串中某字符最后出現的位置,不區分大小寫| 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 ]] )
str_word_count() | 統計字符串含有的單詞數 |mix str_word_count(str $str,[])
strlen() |統計字符串長度 |int strlen(str $str)
count_chars() |統計字符串中所有字母出現次數(0..255) |mixed count_chars ( string $string [, int $mode ] )
md5() |字符串md5編碼 $str = ""Hello""; |echo md5($str)
iconv |
mb_substr |獲取字符串的部分 string mb_substr| ( string $str , int $start [, int $length = NULL [, string $encoding = mb_internal_encoding() ]] )
mb_http_output |設置/獲取 HTTP 輸出字符編碼 |mixed mb_http_output ([ string $encoding = mb_http_output() ] )
mb_strlen |獲取字符串的長度 |mixed mb_strlen ( string $str [, string $encoding = mb_internal_encoding() ] )
iconv |字符串按要求的字符編碼來轉換 |string iconv ( string $in_charset , string $out_charset , string $str )
iconv_substr | 截取字符串的部分 |
iconv_get_encoding | 獲取 iconv 擴展的內部配置變量 |
mb_substr_count |統計字符串出現的次數 |
mb_check_encoding |檢查字符串在指定的編碼里是否有效 |
mb_strrpos |查找字符串在一個字符串中最后出現的位置 |
mb_split | 使用正則表達式分割多字節字符串|
parse_url | 解釋URL成為一個數組 |
- 簡介
- php
- php基礎
- php常用數組函數
- php常用字符串函數
- php魔術方法
- php高階
- swoole
- php優化
- workerman
- PHP底層運行機制和原理
- php框架
- laravel
- 前端
- react
- 爬蟲
- Scrapy
- Linux
- IO復用
- nginx
- nginx進程工作原理
- nginx配置
- 正向代理反向代理
- UPSTREAM
- SERVER
- HTTPS
- queue
- kafka
- redis
- DB
- mysql
- 存儲引擎
- 索引
- 鎖
- 觸發器
- 分庫分表
- 三范式
- 負載均衡
- 事務
- EXPLAN
- mysql死鎖
- mysql索引覆蓋與回表
- mysql聚簇索引與非聚簇索引
- NoSql
- memcache
- redis
- mongo
- 網絡協議
- tcp與udp
- https與http
- 架構
- LNMP架構下HTTP請求的調用次序
- 數據結構&算法
- 基礎數據結構
- Linked List
- array
- stack
- queue
- tree
- hash
- heap
- 常見算法
- 排序算法
- 查找算法
- 其他
- php的一些坑
- 常問面試題
- 技術面試最后反問面試官的話
- hr
- redis緩存擊穿、穿透、雪崩
- 面試中回答的不好的問題
- web攻擊防范