php字串中常用函數的總結(附範例)
這篇文章帶給大家的內容是關於php字串中常用函數的總結(附範例),有一定的參考價值,有需要的朋友可以參考一下,希望對你有幫助。
PHP常用函數:
函數名稱 | 描述 | 實例 |
---|---|---|
trim() | 刪除字串兩端的空格或其他預定義字元 | "$str = "\r\nHello World!\r\n"; echo trim($str); |
rtrim() | 刪除字串右邊的空格或其他預定義字元 | "$str = "Hello World!\n\n"; echo rtrim ($str);" |
chop() | rtrim()的別名 | #同上 |
刪除字串左邊的空格或其他預定義字元 | "$str = "\r\nHello World!"; echo ltrim($str);" | |
迴路中的目錄部分(我們把它歸在字串函數裡了) | echo dirname("c: /testweb/home.php"); | |
把字串填入指定的長度 | $str = "Hello World "; echo str_pad($str,20,"."); | |
重複使用指定字串 | echo str_repeat( ".",13); | |
#把字串分割到陣列中 | print_r(str_split("Hello")) ; | |
反轉字串 | #echo strrev("Hello World!"); | |
依照指定長度對字串進行折行處理 | "$str = ""An example on a long word is: Supercalifragulistic""; echo wordwrap( $str,15);" | |
#隨機地打亂字串中所有字元 | echo str_shuffle("Hello World" ); | |
將字串解析成變數 | "parse_str("id=23&name=John Adams",$myArray) ; print_r($myArray);" | |
透過千位元分組格式化數字 | "echo number_format("1000000" ); echo number_format("1000000",2); echo number_format("1000000",2,"","",""."");" | |
字串轉為小寫 | echo strtolower("Hello WORLD!"); | |
#字串轉為大寫 | echo strtoupper("Hello WORLD!"); | |
字串首字母大寫 | echo ucfirst("hello world"); | |
字串每個單字首字轉為大寫 | echo ucwords( "hello world"); | |
把字元轉換成HTML實體 | $str = ""John & 'Adams'" "; echo htmlentities($str, ENT_COMPAT); | |
預先定義字元轉html編碼 | ||
\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)|
使用字串為標誌分割另一個字串 | array explode(str $sep ,str $str[,int $limit]) | |
同join,將陣列值用預訂字元連接成字串 | string implode ( string $glue , array $pieces ) | |
截取字串 | string substr ( string $string , int $start [, int $length ] ) | |
字串取代操作,區分大小寫 | mix str_replace(mix $search ,,mix $replace,mix $subject[,int &$num]) | |
##字串替換運算,不區分大小寫 | #mix str_ireplace ( mix $search , mix $replace , mix $subject [, int &$count ] ) | |
統計一個字串,在另一個字串中出現次數 | int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] ) | |
取代字串中某字串為另一個字串 | mixed substr_replace ( mixed $string , string $replacement , int $start [, int $length ] ) | |
傳回兩個字串相同字元的數量 | int similar_text(str $str1,str $str2) | |
傳回一個字串在另一個字串中開始位置到結束的字串 | string strstr ( string $str, string $needle , bool $before_needle ) | |
傳回一個字串在另一個字串中最後一次出現位置開始到最後的字串 | string strrchr ( string $haystack , mixed $needle ) | |
傳回一個字串在另一個字串中開始位置到結束的字串,不區分大小寫 | string stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] ) | |
轉換字串中的某些字元 | string strtr ( string $str , string $from , string $to ) | |
尋找字串中某字元最早出現的位置 | int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) | |
尋找字串中某字符最先出現的位置,不區分大小寫 | int stripos ( string $haystack , string $needle [, int $offset ] ) | |
尋找某字串中某字元最後出現的位置 | int strrpos ( string $haystack , string $needle [, int $offset = 0 ] ) | |
尋找某字串中某字元最後出現的位置,不區分大小寫 | int strripos ( string $haystack , string $needle [, int $offset ] ) | |
傳回字串中首次符合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() ] ) | |
字串依要求的字元編碼來轉換 | #string iconv ( string $in_charset , string $out_charset , string $str ) | |
##截取字串的部分 | # | |
取得iconv 擴充的內部設定變數 | ||
#統計字串出現的次數 | ||
#檢查字串在指定的編碼裡是否有效 | ||
尋找字串在一個字串中最後出現的位置 | ||
使用正規表示式分割多位元組字串 | ||
#解釋URL成為一個陣列 |
中文主要用的是GBK和utf-8兩種編碼格式。
GBK和utf-8是兩個不同的編碼委員會對於漢字進行的編碼的標準。
他們規定GBK是雙字節,也就是一個漢字佔用2Bytes。
以上是php字串中常用函數的總結(附範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

在php中,可以使用ord()函數將字元轉成ascii碼,該函數可以傳回單一字元或字串中第一個字元的ASCII值,傳回的ASCII值會以整數形式顯示;轉換語法「ord (string)”,參數“string”不可省略,是要從中獲得ASCII值的字串(或單一字元)。

php字串取代某個字元為空的兩種方法:1、使用str_replace()函數將指定字元替換為空字符,只需要將第一個參數設定為指定字符,第二個參數設定為空字符,語法「str_replace("指定字元","", $str)」;2、用preg_replace()函數配合正規表示式來匹配指定字元並將其替換空字符,語法「preg_replace('/指定字元/', "",$str)」。

兩種去除方法:1、利用preg_replace()執行正規表示式搜尋所有大寫字母並將其替換為空字元即可,語法「preg_replace('/[A-Z]/','',$str)」。 2.利用preg_filter()執行正規表示式搜尋所有大寫字母並將其替換為空字元即可,語法「preg_filter('/[A-Z]/','',$str)」。

PHP是一種強制型別類型程式語言,常被用來開發Web應用程式。在網路開發過程中,您可能需要對字串執行各種操作,例如從字串中刪除特定字符,保留字串中的數字或字母等。在本文中,我們將重點放在PHP中如何刪除字串左側或右側的特定字元。

兩種方法:1、用preg_match_all()配合正規過濾字串,語法“preg_match_all("/[\x{4e00}-\x{9fff}]+/u","$str",$arr);” ;2、用preg_replace()配合正規搜尋字串中的非中文字母,並將其替換成空字符,語法「preg_replace("/[^\x{4E00}-\x{9FFF}]+/u" ,'',$str)」。

php可以為字串添加字元。兩種實作方法:1、使用字串連接符號「.」將指定字元拼接到字串的開頭或結尾即可,語法「指定字元. 字串」或「字串. 指定字元」;2、使用substr_replace ()函數在字串的指定位置插入指定字元即可,語法“substr_replace(字串,指定字元,指定位置,0)”,指定位置的值可以是0、負值和正值。

PHP是一種非常受歡迎的程式語言,也是建立動態網站的首選工具之一。在PHP開發中,我們經常需要操作字串,其中一個常見的需求是去除字串中的雙引號。在這篇文章中,我們將介紹一些方法來移除PHP字串中的雙引號。

實作步驟:1、利用str_split()函數將字串轉為字元數組,語法「str_split(字串)」;2、使用asort()或arsort()函數來對字元數組進行升序排序或降序排序,語法「asort(字元陣列)」或「arsort(字元陣列)」;3、使用implode()函數將排序後的字元陣列轉回字串,語法「implode(排序後字元陣列)」。
