字符串處理函數

 

  1. <?php 
  2.     //字符串處理函數 
  3.     //確定字符串長度strlen 
  4.     //$str = "helloworld"; 
  5.     //echo strlen($str); 
  6.  
  7.     //比較兩個字符串是否相等 strcmp() 區分大小寫 
  8.     /* 
  9.     $str1 = "helloworld"; 
  10.     $str2 = "HelloworlD"; 
  11.     if(strcmp($str1,$str2) === 0){ 
  12.         echo "字符串相同"; 
  13.     }else{ 
  14.         echo "字符串不相同"; 
  15.     } 
  16.     */ 
  17.  
  18.     //比較兩個字符串是否相等,不區分大小寫 strcasecmp 
  19.     /* 
  20.     $str1 = "helloworld"; 
  21.     $str2 = "HelloworlD"; 
  22.     if(strcasecmp($str1,$str2) === 0){ 
  23.         echo "字符串相同"; 
  24.     }else{ 
  25.         echo "字符串不相同"; 
  26.     } 
  27.     */ 
  28.  
  29.     //將字符串全部轉換成小寫 
  30.     /* 
  31.     $str = "HElloWorld"; 
  32.     echo strtolower($str); 
  33.     */ 
  34.  
  35.     //將字符串全部轉換爲大寫 
  36.     /* 
  37.     $str= "helloworld"; 
  38.     echo strtoupper($str); 
  39.     */ 
  40.  
  41.     //將字符串的第一個字符大寫 
  42.     /* 
  43.     $str = "helloworld"; 
  44.     echo ucfirst($str); 
  45.     */ 
  46.  
  47.     //將字符串的每一個單詞的首字母大寫 
  48.     /* 
  49.     $str = "hello world php"; 
  50.     echo ucwords($str);  
  51.     */ 
  52.  
  53.     //將HTML轉換爲純文本strip_tags() 
  54.     /* 
  55.     $str = "<a href='dsada'>fdsfsdfsdfsf發倒薩發送到</a>"; 
  56.     echo strip_tags($str); 
  57.     */ 
  58.  
  59.     //將字符串分割爲數組explode 
  60.     /* 
  61.     $str = "a,b,c,f"; 
  62.     print_r(explode(",",$str,3)); 
  63.     */ 
  64.  
  65.     //將數組轉換爲字符串implode 
  66.     /* 
  67.     $arr = array('a','b','c'); 
  68.     echo implode('',$arr); 
  69.     */ 
  70.  
  71.     //查找子字符串在被查找字符串中第一次出現的位置strpos()區分大小寫 stripos()不區分大小寫 
  72.     /* 
  73.     $str = "helloworld"; 
  74.     echo strpos($str,'h'); 
  75.     */ 
  76.  
  77.     //找到字符串最後一次出現的位置strrpos() 區分大小寫 
  78.     /* 
  79.     $str = "helloworld"; 
  80.     echo strrpos($str,'o'); 
  81.     */ 
  82.  
  83.     //用另一個字符串替換字符串的所有實例str_replace($search,$sreplace,$subject) 
  84.     /* 
  85.     $str = "helloworld"; 
  86.     $replace = "<b>o</b>"; 
  87.     echo str_replace('o',$replace,$str); 
  88.     */ 
  89.      
  90.     //獲取字符串的一部分,匹配開始到原字符串串結束strstr() 
  91.     /* 
  92.     $str = "o"; 
  93.     echo strstr("helloworld",$str); 
  94.     */ 
  95.  
  96.     //根據預定義的偏移返回字符串的一部分 
  97.     /* 
  98.     //其中length 決定方向,start確定位置 
  99.     $str = "helloworld"; 
  100.     echo substr($str,0,2); 
  101.     */ 
  102.  
  103.     //確定字符串出現的頻率substr_count() 
  104.     /* 
  105.     $str = "helloworld"; 
  106.     echo substr_count($str,"h"); 
  107.     */ 
  108.  
  109.     //用另一個字符串替換一個字符串的一部分 
  110.     /* 
  111.     $str = "helloworld"; 
  112.     echo substr_replace($str,"php",5); 
  113.     */ 
  114.  
  115.     //填充和剔除字符串ltrim(),rtrim(),trim() 
  116.  
  117.  
  118.      
  119.  
  120.  
  121. ?> 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章