置換後の文字列 = str_replace(置換前の文字列(配列可),置換後の文字列(配列可),元の文字列(配列可)[,置換した回数(PHP5より)]);
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
例:
<?php header("Content-type: text/html; charset=utf-8"); echo str_replace("桜","薔薇","桜が咲いた、梅も咲いた"); ?>
薔薇が咲いた、梅も咲いた
<?php header("Content-type: text/html; charset=utf-8"); $before = array("桜","梅"); $after = array("チューリップ","タンポポ"); echo str_replace($before,$after,"桜が咲いた、梅も咲いた"); ?>
チューリップが咲いた、タンポポも咲いた
<?php header("Content-type: text/html; charset=utf-8"); $before = array("桜","梅"); $after = "すみれ"; echo str_replace($before,$after,"桜が咲いた、梅も咲いた"); ?>
すみれが咲いた、すみれも咲いた
<?php header("Content-type: text/html; charset=utf-8"); $data = array("桜","桜の木","桜色"); foreach(str_replace("桜","桃",$data) as $value){ echo $value . "<br>"; } ?>
桃
桃の木
桃色
取り出した文字列 = substr(元の文字列,取り出す開始位置[,取り出す文字数])
string substr ( string $string , int $start [, int $length ] )
<?php header("Content-type: text/html; charset=utf-8"); $moji = "aiueokakiku"; echo substr($moji,4,2); ?>
ok
取り出した文字列 = mb_substr(元の文字列,取り出す開始位置[,取り出す文字数][,文字エンコーディング(省略:内部円コーディング)])
string mb_substr ( string $str , int $start [, int $length [, string $encoding ]] )
<?php header("Content-type: text/html; charset=utf-8"); $moji = "あいうえおかきくけこ"; echo mb_substr($moji,4,2,"utf-8"); ?>
おか
<?php header("Content-type: text/html; charset=utf-8"); $moji = "aiueokakiku"; //終わりから8番目の文字から、終わりから3番目までを除く echo substr($moji,-8,-3); ?>
eokak
<?php header("Content-type: text/html; charset=utf-8"); $moji = "aiueokakiku"; //頭から2番目の文字 echo $moji{2}; //頭から6番目の文字 echo $moji{6}; ?>
ua
<?php header("Content-type: text/html; charset=utf-8"); $moji = "aiueokakiku"; //4番目の文字から後ろすべての文字列 echo substr($moji, 4); ?>
okakiku
<?php header("Content-type: text/html; charset=utf-8"); $moji = "aiueokakiku"; //1番後ろの文字 echo substr($moji, -1); ?>
u
<?php header("Content-type: text/html; charset=utf-8"); $moji = "aiueokakiku"; //終わりから6番目の文字から2桁 echo substr($moji, -6, 2); ?>
ka
printf()関数と、sprintf()関数の違いは、printf()ではそのまま整形された内容を出力し、sprintf()では整形された文字列が戻り値となることです。
printf(フォーマット,整形したい文字列や数値[,整形したい文字列や数値...]);
int printf ( string $format [, mixed $args [, mixed $... ]] )
整形後の文字列 = sprintf(フォーマット,整形したい文字列や数値[,整形したい文字列や数値...]);
string sprintf ( string $format [, mixed $args [, mixed $... ]] )
最初についている「%」は、その後にに続くフォーマットに従って整形しますという意味です。
<?php header("Content-type: text/html; charset=utf-8"); $data = "123456.78901"; // f: 引数は浮動小数点 // .2 小数点以下の数値を2桁(.02)で表現します。 printf("%.2f<br>",$data); printf("%.4f<br>",$data); printf("%.6f<br>",$data); ?>
123456.79
123456.7890
123456.789010
<?php header("Content-type: text/html; charset=utf-8"); // d 引数は整数 // 2 2桁に整形します // 0 指定した桁に満たなかったら0で表現します printf("%02d月%02d日", 3, 9); ?>
03月09日
<?php header("Content-type: text/html; charset=utf-8"); // s 引数は文字列 printf("%sです。%sです♪%sです!","ネコ","17歳","オス"); ?>
ネコです。17歳です♪オスです!
指定したフォーマットに従って文字列の中の値を取得します。
配列 = sscanf(文字列,フォーマット[,変数,変数,変数]);
mixed sscanf ( string $str , string $format [, mixed &$... ] )
例:
<?php header("Content-type: text/html; charset=utf-8"); $no = sscanf("No:0001,No:0002","No:%d,No:%d"); echo $no[0]; echo $no[1]; ?>
12
<?php header("Content-type: text/html; charset=utf-8"); $no = sscanf("No:0001,No:0002","No:%d,No:%d",$value1,$value2); echo $value1; echo $value2; ?>
12
<?php header("Content-type: text/html; charset=utf-8"); $no = sscanf("2011年03月10日","%d年%d月%d日",$a,$b,$c); echo $a; echo $b; echo $c; ?>
2011310