【 文字列関数 】

1. bin2hex
2. crypt
3. explode
4. htmlentities と str_replace
5. implode
6. number_format
7. strstr

概要
  • バイナリデータを16進表現に変換します

  • SHIFT_JIS の 5C データの存在を知るのに有効です



  • print $_POST['test'] . "<BR>";
    print bin2hex($_POST['test']) . "<BR>";
    


    表示
    955c5c8ea6
    


  • magic_quotes_gpc = Off の場合は以下のようになります
  • 表示
    955c8ea6
    


    概要
  • 文字列の一方向の暗号化(ハッシュ化)を行います



  • print crypt("lightbox","myencode" );
    


    my3K1oMV2agOU
    
  • パスワードをファイル等に書き込む場合に、元のパスワード文字列を知る事ができないようにします


  • 概要
  • VB での split 関数に相当します



  • $a = explode( ",", "A,B,C" );
    print_r( $a );
    


    Array
    (
        [0] => A
        [1] => B
        [2] => C
    )
    


    概要
  • htmlentities は、HTML コードを表示する際に有効です

  • str_replace は、その結果をさらに実際の変換結果にする為に使用しています



  • $a = htmlentities("<INPUT style=\"width:10\">");
    $b = str_replace( "&", "&amp;", $a );
    print $b;
    


    &lt;INPUT style=&quot;width:10&quot;&gt; 
    

    概要
  • VB での join 関数に相当します

  • join は、implode の別名として存在します



  • $a = implode( ",", array("A","B","C") );
    print $a;
    


    A,B,C 
    


    概要
  • 数値編集を行ないます



  • $a = number_format( 1234.567, 2, ".", "," );
    print $a;
    


    1,234.57 
    


    概要
  • 文字列中に任意の文字列があるかどうかを調べます

  • 見つかった場合は、見つかった位置から最後までの文字列を返します



  • $a = strstr("abcdefghijk", "def" );
    print $a . "<BR>";
    if ( $a ) {
       print "hit!";
    }
     
    $a = strstr("abcdefghijk", "xxx" );
    print $a . "<BR>";
    if ( !$a ) {
       print "out!";
    }
    


    defghijk
    hit!
    out!