php文字列の一般的な組み込み関数

36231 ワード

学習関数のアウトライン1.作用2.文法3.例
文書ディレクトリ
  • 文字列共通関数分類
  • 1.文字列長関数
  • 2.検索文字列位置関数
  • 3.文字列置換関数
  • 4.文字列抽出関数
  • 5.分割、接続、反転関数
  • 6.削除、スペース関数
  • を追加
  • 7.文字エスケープ関数
  • 8.文字列比較関数
  • 9.変換大文字と小文字の関数
  • 文字列共通関数の分類
  • 文字列長関数
  • 文字列位置関数
  • を検索
  • 文字列置換関数
  • 文字列文字関数
  • を抽出
  • 分割、接続、反転関数
  • 削除、スペース関数
  • を追加
  • htmlコード/データベースセキュリティ処理関連関数
  • 文字関数
  • の比較
  • 文字大文字小文字変換関数
  • 1.文字列長関数
    strlen  :      o(1),  zval    value  len
      :        
      :strlen(string)
    string	  。         。
    
      :
    $str = 'hello';
    echo strlen($str); //5 
    $str = ' ';
    echo strlen($str); //3
    echo mb_strlen($str,'utf-8'); //1
    
        strlen() mb_strlen()
    strlen:
         3   ,  uft-8  3         
    mb_strlen:
         utf-8   ,        1
         gbk  gb2312   ,        2
    mb_internal_encoding():      
    

    2.文字列位置関数の検索
    strpos  
      :                。
      :strpos(string,find,start)
    string    	  。         。 
    find    	  。         。
    start    	  。         。
      :                    。
          。          。
    
      :
    $str = 'abc';
    $char = 'c';
    strpos($str,$char); //2
    
     0   ,     bool  false,          ===
    
    stripos(),      
    strrpos()

    3.文字列置換関数
    str_replace(),str_replace(find,replace,string,count)
    find    	  。       。
    replace    	  。     find      。 
    string    	  。         。
    count    	  。           。
      :                (     )。
              .
               ,        。
               ,                    。
                    ,                      ,
                    。
            ,        ,                   。
    
      :
            count     str_replace()$arr = array("blue","red","green","yellow");
    print_r(str_replace("red","pink",$arr,$i));
    echo "Replacements: $i";
    //Array ( [0] => blue [1] => pink [2] => green [3] => yellow ) 
    //Replacements: 1
    
                         str_replace()$find = array("Hello","world");
    $replace = array("B");
    $arr = array("Hello","world","!");
    print_r(str_replace($find,$replace,$arr));
    //Array ( [0] => B [1] => [2] => ! )
    
    str_ireplace(),      
    
    strtr()  :
      :           
      :strtr(string,from,to)  strtr(string,array)
    string	  。         。
    from	  (      )。        。
    to		  (      )。         。
    array	  (     from   to)。  ,             ,          。
      :           。   from   to        ,            。
      :
    $str = '  ,  ,  ,  ';
    echo strtr($str,' ',' ') //
    echo strtr($str,[' '=>' ',' '=>' ']);
    
    substr_replace()  :(       substr)
      :               
      :substr_replace(string,replacement,start,length)
      :                 。   start        length        start,  length   0。          。
    
      :
    echo substr_replace("Hello world","baicai",6); //Hello baicai
    

    4.文字列文字関数の抽出
    substr()  
      :     
      :substr(string,start,length)
    string	  。              。
    start	  。           。   --0 -               。
    length	  。           。           。
       -   start             ,   -(     length   end   ,      , 0  ,   end    ,
       [start,end))
    
    
      :         。   start        length       start,
      length   0:
    $str = 'this is a test';
    echo substr($str, 0, 3);
    echo substr($str, 0, -3);
    echo substr($str, -10, -3);
    
    strstr()   (  strchr)
      :                ,   ,           ,     FALSE。          。
      :strstr(string,search,before_search)
    string	  。         。
    search	  。         。        ,            ASCII     。
    before_search	  。       "false""true",     search                。
    
      :          。             ,    stristr()strrchr()               .
      :
    echo strstr("Hello world!","world");  //    world!
    

    5.分割、接続、反転関数
    explode()  
      :         
      :explode(separator,string,limit)
    separator	  。          。
    string    	  。       。
    limit	  。             。
        :
       0 -        limit       ,
       0 -           -limit              ,
    0 -            
    
      :         。"separator"          。          。
    
      :
    $arr = "Learning PHP is a good choice";
    print_r(explode(" ", $arr));
    
    implode()  :
      :           
      :implode(separator,array)
    separator     。             。    ""(    )。
    array    	  。          。
    
      :
    $arr = array('Hello','World!','I','love','PHP!');
    echo implode(" ",$arr);
    
    str_split()  :
      :          
      :str_split(string,length)
    string	  ,       
    length	  ,           ,   1,  length  1false,  length            ,                  。
      :               
    
      :
    $str = "helloworld";
    $arr = str_split($str,3);
    print_r($arr);
    //Array ( [0] => hel [1] => low [2] => orl [3] => d )
    
    strrev()  :
      :     
      :strrev(string)
      :         。
      :
    $i = "hello world";
    $j = strrev($i);
    echo $j;
    //dlrow olleh
    

    6.削除、スペース関数の追加
    string trim ( string $str [, string $charlist ] )
      :                  
    string	             
    charlist	  ,              ,
        ,         :"\0" -- NULL,"\t" --    ,"
    "
    -- ,"\x0B" -- ,"\r" -- ," " -- string ltrim ( string $str [, string $charlist ] ) string rtrim ( string $str [, string $charlist ] ) : $i = " hello world "; echo " trim ".$i; echo "
    "
    ; $j = trim($i); echo " trim ".$j; chunk_split() : : 。 :chunk_split(string,length,end) string 。 。 length 。 , 。 76。 end 。 , 。 \r\n: $str = "Hello php.cn!"; echo chunk_split($str,3,"..."); //Hel...lo ...php....cn...!... str_pad() : : :str_pad(string,length,pad_string,pad_type) string , , length , , , 。 pad_string , , 。 pad_type , , STR_PAD_BOTH -- , , 。STR_PAD_LEFT --STR_PAD_RIGHT -- , 。 : $i = "hello world"; $j = str_pad($i,30,"!"); // , echo $j; echo "
    "
    ; $k = "hello world"; $h = str_pad($k,30,".",STR_PAD_LEFT);// echo $h; //hello world!!!!!!!!!!!!!!!!!!! //...................hello world $k = "hello world"; $h = str_pad($k,20,".",STR_PAD_BOTH);// echo $h; //....hello world.....

    7.文字エスケープ関数
    addslashes ( string $str )
      :                   。
          :
       (')
       (")
       (\)
    NULL
      :                                。
    
    stripslashes ( string $str )
        addslashes()         。
      :                 HTML         。
    
    htmlspecialchars ——              HTML   
    htmlspecialchars_decode —— htmlspecialchars()HTML        
    
    html_entity_decode —— htmlentities ()HTML        
    htmlentities ——                 HTML   
    

    8.文字列比較関数
     strcmp()  
      :       (     )
      :strcmp(string1,string2)
    string1	  ,            。
    string2	  ,            。
      :strcmp()strcmp()          ,       。         ,   0,  string1  string2,    -1,  string1  string2,    1:
    $i = "Hello world";
    $j = "HELLO WORLD";
    echo strcmp($i,$j);
    //1
    
    strcasecmp()   (     )
    

    9.大文字と小文字の変換
    strtolower() 
      :        
    strtoupper()
      :        
    ucfirst() 
      :             
    ucwords() 
      :