文字列を分割します。
配列 = explode(区切りにする文字,元の文字列[,区切り回数]);
array explode ( string $delimiter , string $string [, int $limit ] )
例;
<?php header("Content-type: text/html; charset=utf-8"); $hiduke = "2011/03/10"; $ymd = explode("/",$hiduke); echo $ymd[0]; echo $ymd[1]; echo $ymd[2]; ?>
20110310
オプションを指定すると、区切った時にできる配列の要素の数が指定できます。
<?php header("Content-type: text/html; charset=utf-8"); $hiduke = "2011/03/10"; $ymd = explode("/",$hiduke,1); echo $ymd[0]; ?>
2011/03/10
<?php header("Content-type: text/html; charset=utf-8"); $hiduke = "2011/03/10"; $ymd = explode("/",$hiduke,2); echo $ymd[0]; echo $ymd[1]; ?>
201103/10
<?php header("Content-type: text/html; charset=utf-8"); $hiduke = "2011/03/10"; $ymd = explode("/",$hiduke,3); echo $ymd[0]; echo $ymd[1]; echo $ymd[2]; ?>
20110310
<?php header("Content-type: text/html; charset=utf-8"); $hiduke = "2011/03/10"; //「/」で区切られた要素から-2個分の要素のみ取り出す $ymd = explode("/",$hiduke,-2); echo $ymd[0]; ?>
2011
文字列を結合する。
結合後の文字列 = implode(結合する文字列,配列);
string implode ( string $glue , array $pieces ) string implode ( array $pieces )
例:
<?php header("Content-type: text/html; charset=utf-8"); $hiduke = "2011/03/10"; $ymd = explode("/",$hiduke); $hiduke = implode("-",$ymd); echo $hiduke; ?>
2011-03-10
同じ文字を反復します。
反復後の文字列 = str_repeat(文字列,反復回数);
string str_repeat ( string $input , int $multiplier )
例:
<?php header("Content-type: text/html; charset=utf-8"); echo str_repeat("0",100); ?>
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
<?php header("Content-type: text/html; charset=utf-8"); echo "回答A:" . str_repeat("*",5) . "<br>"; echo "回答B:" . str_repeat("*",20) . "<br>"; ?>
回答A:*****
回答B:********************
空白文字を除去する。
空白除去の文字列 = trim(文字列[,空白文字以外で除去したい文字]);
string trim ( string $str [, string $charlist ] )
空白文字として除去される文字
空白文字 | 意味 |
" " | 通常の空白 |
"\t" | タブ |
"\n" | リターン |
"\r" | 改行 |
"\0" | NULLバイト |
"\x0B" | 垂直タブ |
<?php header("Content-type: text/html; charset=utf-8"); $text = "abc "; echo trim($text); ?>
abc
<?php header("Content-type: text/html; charset=utf-8"); $text = "abc "; echo trim($text,"ab"); ?>
c
文字列を全角、半角、カナ変換などができます。
返還後の文字列 = mb_convert_kana(変換後の文字列[,変換オプション文字(デフォルト:KV)][,円コーディング]);
string mb_convert_kana ( string $str [, string $option = "KV" [, string $encoding ]] )
省略時の文字エンコーディングには、内部エンコーディングが使用されます。
例:
<?php header("Content-type: text/html; charset=utf-8"); $kana = mb_convert_kana("アイウエオ","KV","utf-8"); echo $kana; ?>
アイウエオ
<?php header("Content-type: text/html; charset=utf-8"); $mail = mb_convert_kana("aaa@b123bb.com","a","utf-8"); echo $mail; ?>
aaa@b123bb.com
変換オプション文字 | 変換内容 |
r | 全角英字→半角英字 |
R | 半角英字→全角英字 |
n | 全角数字→半角数字 |
N | 半角数字→全角数字 |
a | 全角英数字→半角英数字 |
A | 半角英数字→全角英数字 |
s | 全角スペース→半角スペース |
S | 半角スペース→全角スペース |
k | 全角カタカナ→半角カタカナ |
K | 半角カタカナ→全角カタカナ |
h | 全角ひらがな→半角カタカナ |
H | 半角カタカナ→全角ひらがな |
c | 全角カタカナ→全角ひらがな |
C | 全角ひらがな→全角カタカナ |
V | 濁点付きの文字を一文字にする("K","H"と共に使用) |
文字数を取得する。
長さ = strlen(文字列); 長さ = mb_strlen(文字列);
int strlen ( string $string ) int mb_strlen ( string $str [, string $encoding ] )
例:
<?php header("Content-type: text/html; charset=utf-8"); $kana = "アイウエオ"; echo strlen($kana) . "<br>"; echo mb_strlen($kana,"utf-8") . "<br>"; ?>
15
5
<?php header("Content-type: text/html; charset=utf-8"); $kana = "aaa@b123bb.com"; echo strlen($kana) . "<br>"; echo mb_strlen($kana,"utf-8") . "<br>"; ?>
14
14