PHP經常使用函數之字符串處理

字符串大小寫轉換

  • strtoupper(string $str) //把字符串所有轉換成大寫字母html

  • strtolower(string $str) //把字符串所有轉換成小寫字母json

  • ucfirst(string $str) //把字符串的首字母轉換成大寫數組

  • ucwords(string $str) //把字符串中所有以空格分隔的單詞首字母轉換爲大寫app

字符串去除空格

  • ltrim() //去除字符串左邊空格編碼

  • rtrim() //去除字符串右邊空格加密

  • trim() //去除字符串兩邊空格code

字符串格式化

  • nl2br() //在字符串換行以前插入 HTML 換行標記<br/> (字符串必須用雙引號)htm

  • strrev() //字符串反轉對象

  • md5() //將字符串進行MD5加密,返回一個32位十六進制字符串ip

  • htmlspecialchars(string $str) //將字符串中的html標籤轉換成html實體

  • stripslashes(string $str) //將字符串中的去掉

  • strip_tags (string $str[,string $allowable_tags]) //從字符串中去除 HTML 和 PHP 標記(HTML 註釋和 PHP 標籤是硬編碼,不能經過$allowable_tags修改的)

  • wordwrap () //使用字符串打斷字符串爲指定數量的字串

  • strrev (string $string) //反轉字符串

獲取字符串長度

  • strlen(string $str)

字符串輸出

  • echo()

  • print()

  • die()/exit()

  • printf()

  • sprintf()

字符串比較

  • strcmp(string $str1,string $str2) //區分大小寫比較

  • strcasecmp(string $str1,string $str2) //不區分大小寫比較

字符串替換

  • str_replace() //在$subject搜索$search替換爲$replace的字符串或數組

    一、$search和$replace都是字符串
    示例:str_replace("a", "apple", "I like eat apple.");
    說明:在$subject中將$search字符串替換爲$replace字符串便可。
    返回:"I like eapplet applepple."。
    二、$search爲數組
    示例:str_replace(array("a", "p"), "apple", "I like eat apple.");
    說明:在$subject中將$search數組中的各個字符串元素依次分別替換爲中$replace字符串便可。
    返回:"I like eaappleapplelet aappleappleleappleapplele."。
    三、$replace爲數組
    示例:str_replace("a", array("apple", "pear"), "I like eat apple.");
    說明:在$subject中將$search字符串替換爲Array便可。
    返回:"I like eArrayt Arraypple."。
    四、$subject爲數組
    示例:str_replace("a", "apple", array("I like eat app.", "I like eat pear."));
    說明:返回值爲數組,將$subject中的各元素分別用$search替換爲$replace的值進行替換。
    返回:Array ( [0] => I like eapplet applepp. [1] => I like eapplet peappler. )。
    五、$search和$replace都爲數組
    示例:str_replace(array("a", "p"), array("apple", "pear"), "I like eat apple.");
    說明:在$subject中將$search的各元素值依次分別替換爲$replace的各元素值。
    返回:"I like eapearpearlet apearpearlepearpearle."
    其中:
    若是$search的數組值比$replace的數組值長,
    str_replace(array("a", "p", "b"), array("apple", "pear"), "I like eat apple && b.");
    將$search中多出來的數組元素在$replace中匹配的字符串替換爲空串,返回"I like eapearpearlet apearpearlepearpearle && ."。
    若是$replace的數組值比$search的數組值長,
    str_replace(array("a", "p"), array("apple", "pear", "banana"), "I like eat apple && b.");
    $replace中多出來的數組元素將被忽略。返回"I like eapearpearlet apearpearlepearpearle && b."。
    六、$search、$replace和$subject都爲數組
    示例:str_replace(array("a", "p"), array("apple", "pear"), array("I like eat apple.", "I like eat pear"));
    說明:在$subject中將$search各元素的值依次分別替換爲$replace的值,而後進行替換。

    返回:Array ( [0] => I like eapearpearlet apearpearlepearpearle. [1] => I like eapearpearlet peareapearpearler )

字符串重複

  • str_repeat(string $input,int $multiplier)

字符串轉化

  • str_split (string $string[,int $split_length = 1]) //將字符串轉換爲數組

  • explode ( string $delimiter , string $string [, int $limit ] ) //使用一個字符串分割另外一個字符串轉換成數組

  • json_decode ( string $json [, bool $assoc = false [, int $depth = 512 [, int $options = 0 ]]] ) //將一個 JSON 編碼的字符串解碼轉換爲 PHP 變量 (對象或數組)

字符串截取

  • substr ( string $string , int $start [, int $length ] ) //返回字符串的子串

  • strstr (string $haystack ,mixed $needle[, bool $before_needle = false ]) //在字符串中,查找字符串的首次出現的位置開始截取到末尾

  • strrchr ( string $haystack , mixed $needle ) //查找指定字符在字符串中的最後一次出現位置開始截取到末尾,通常用於獲取文件後綴

字符串打亂

  • str_shuffle ( string $str ) //隨機打亂一個字符串

相關文章
相關標籤/搜索