よく使うphpの文字列操作組み込み関数集
- php
- 組み込み関数
- 文字列
- string
関数(引数):返り値 | 概要 | 例 |
---|---|---|
explode ( string $delimiter , string $string [, int $limit = PHP_INT_MAX ] ) : array | string を文字列 delimiter で区切った文字列配列を返す | explode(" ", "a b c"); // ["a","b","c"] |
implode( string $glue , array $pieces ) : string | 配列piecesの要素をglue文字列で連結した文字列を返す。 | implode(",", ["a","b","c"]) // "a,b,c" |
nl2br ( string $string [, bool $is_xhtml = TRUE ] ) : string | string に含まれるすべての改行文字 (\nなど)の前に<br />を挿入して返す。 | nl2br("foo isn't\n bar"); // foo isn't<br />\n bar |
strlen ( string $string ) : int | string の長さを返す。 | strlen($str); // 6 |
strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int | 文字列 haystack の中で、needle が最初に現れる位置を探す。 見つからない場合false。 | strpos('abc' ,'a'); // 0 strpos('abc' ,'e'); // false strpos('abcdef abcdef', 'a', 1); // 7 |
substr ( string $string , int $start [, int $length ] ) : string | string の、start で指定された位置から length バイト分の文字列を返す。 | substr("abcdef", 1); // "bcdef" substr("abcdef", -1); // "ef" substr("abcdef", 0, 4); //→ "abcd" |
str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ): mixed | subject の中の search を全て replace に置換する。 | str_replace("apple", "orange", "apple juice"); //-> "orange juice" |
substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] ): int | 文字列 haystack の中での副文字列 needle の出現回数を返す。 offset:オフセット, length:最大長 | $text = 'This is a test'; substr_count($text, 'is'); // 2 substr_count($text, 'is', 3); // 1 substr_count($text, 'is', 3, 3); // 0 |