- <?php
- //字符串處理函數
- //確定字符串長度strlen
- //$str = "helloworld";
- //echo strlen($str);
- //比較兩個字符串是否相等 strcmp() 區分大小寫
- /*
- $str1 = "helloworld";
- $str2 = "HelloworlD";
- if(strcmp($str1,$str2) === 0){
- echo "字符串相同";
- }else{
- echo "字符串不相同";
- }
- */
- //比較兩個字符串是否相等,不區分大小寫 strcasecmp
- /*
- $str1 = "helloworld";
- $str2 = "HelloworlD";
- if(strcasecmp($str1,$str2) === 0){
- echo "字符串相同";
- }else{
- echo "字符串不相同";
- }
- */
- //將字符串全部轉換成小寫
- /*
- $str = "HElloWorld";
- echo strtolower($str);
- */
- //將字符串全部轉換爲大寫
- /*
- $str= "helloworld";
- echo strtoupper($str);
- */
- //將字符串的第一個字符大寫
- /*
- $str = "helloworld";
- echo ucfirst($str);
- */
- //將字符串的每一個單詞的首字母大寫
- /*
- $str = "hello world php";
- echo ucwords($str);
- */
- //將HTML轉換爲純文本strip_tags()
- /*
- $str = "<a href='dsada'>fdsfsdfsdfsf發倒薩發送到</a>";
- echo strip_tags($str);
- */
- //將字符串分割爲數組explode
- /*
- $str = "a,b,c,f";
- print_r(explode(",",$str,3));
- */
- //將數組轉換爲字符串implode
- /*
- $arr = array('a','b','c');
- echo implode('',$arr);
- */
- //查找子字符串在被查找字符串中第一次出現的位置strpos()區分大小寫 stripos()不區分大小寫
- /*
- $str = "helloworld";
- echo strpos($str,'h');
- */
- //找到字符串最後一次出現的位置strrpos() 區分大小寫
- /*
- $str = "helloworld";
- echo strrpos($str,'o');
- */
- //用另一個字符串替換字符串的所有實例str_replace($search,$sreplace,$subject)
- /*
- $str = "helloworld";
- $replace = "<b>o</b>";
- echo str_replace('o',$replace,$str);
- */
- //獲取字符串的一部分,匹配開始到原字符串串結束strstr()
- /*
- $str = "o";
- echo strstr("helloworld",$str);
- */
- //根據預定義的偏移返回字符串的一部分
- /*
- //其中length 決定方向,start確定位置
- $str = "helloworld";
- echo substr($str,0,2);
- */
- //確定字符串出現的頻率substr_count()
- /*
- $str = "helloworld";
- echo substr_count($str,"h");
- */
- //用另一個字符串替換一個字符串的一部分
- /*
- $str = "helloworld";
- echo substr_replace($str,"php",5);
- */
- //填充和剔除字符串ltrim(),rtrim(),trim()
- ?>