Str クラス

The Str class is a set of methods to help with the manipulation of strings.

The class also contains multibyte agnostic versions of PHP's multibyte-aware functions in category 2, as defined on this page. They are drop-in replacements for both versions. For example, you can replace both strlen() and mb_strlen() with Str::strlen(), which will return a multi-byte aware result based on whether or not PHP's mbstring extension is loaded.

The php.ini setting mbstring.func_overload is not supported by FuelPHP. The framework refuses to start when it is set!

increment($str, $first = 1)

increment メソッドは、文字列の後ろに数字を付加したり、既に数字が付いている場合はそれに 1 加算します。

静的 はい
パラメータ
パラメータ デフォルト 説明
$string 必須 対象の文字列。
$first
1
加算を開始する最初の番号。
返り値 string
$string = "filename";
Str::increment($string); // 返り値: filename_1

$string = "filename_1";
Str::increment($string); // 返り値: filename_2

$string = "filename";
Str::increment($string, 3); // 返り値: filename_3

random($type = 'alnum', $length = 8)

random メソッドは、type で指定された文字種のランダムな文字列を生成します。

静的 はい
パラメータ
パラメータ デフォルト 説明
$type alnum 生成する文字列の文字種。alnum 、numeric 、nozero 、 alpha 、 distinct 、 hexdec 、 unique 、sha1 と uuid から選択します。
$length
16
生成したい文字列の文字数 (unique 、sha1 と uuid はこのパラメータを無視します) 。
返り値 string
// alnum (アルファベット大文字、小文字、数字)
Str::random('alnum', 16);
// 返り値: SvZi9Dh3lq7zQYim

// numeric (数字のみ)
Str::random('numeric', 16);
// 返り値: 1045343964672481

// nozero (0 を除いた数字)
Str::random('nozero', 16);
// 返り値: 3244623373994515

// alpha (アルファベット大文字と小文字)
Str::random('alpha', 16);
// 返り値: LuVAXbmxQbbWoYqz

// distinct (読み間違えづらいアルファベット大文字と数字)
Str::random('distinct', 16);
// 返り値: R79MPKMH4KTRN35J

// hexdec (16進数の文字列: a-f, 0-9)
Str::random('hexdec', 16);
// 返り値: 09c34e42f36547f8

// unique (md5 を基にした 32 文字の文字列)
Str::random('unique');
// 返り値: ed4bb844a35b7a4edb7eed0d3795d328

// sha1 (sha1 を基にした 40 文字の文字列)
Str::random('sha1');
// 返り値: af5c5a8cc3be9a3180205c1ed2975015cd6cf1e7

// uuid (バージョン 4 - 擬似乱数)
Str::random('uuid');
// 返り値: f47ac10b-58cc-4372-a567-0e02b2c3d479

truncate($string, $limit, $continuation = '...', $is_html = false)

truncate メソッドは、文字列を指定した長さで切り詰めます。文字列が HTML を含む場合は、それを破壊しないようにすることもできます。

静的 はい
パラメータ
パラメータ デフォルト 説明
$string 必須 切り詰める文字列。
$limit 必須 文字列の最大長 (文字列の長さがこの値を超えると切り詰める) 。
$continuation
'...'
切り詰めた文字列の末尾に付加する文字列。
$is_html
false
文字列が HTML を含む場合、この引数を true にすると、このメソッドは HTML タグを破壊しません。
返り値 string
$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
Str::truncate($string, 15); // 返り値: Lorem ipsum dol...

$string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
Str::truncate($string, 15, '...Read More'); // 返り値: Lorem ipsum dol...Read More

alternator(*$args)

このメソッドに引数として与えた値を順番に出力するクロージャを返す。 引数に false を渡してクロージャを呼ぶときに限って、 次に移動しないで値を返し、次回の呼び出しには同じ値を返す。

静的 はい
パラメータ
パラメータ デフォルト 説明
*$args 必須 順番に出力するのと同数の引数。
返り値 Closure
$alt = Str::alternator('one', 'two', 'three', 'four');
echo $alt(); // 'one' を出力
echo $alt(); // 'two' を出力
echo $alt(false); // 'three' を出力、次回の呼び出しを見るとわかるように次には移動しません
echo $alt(); // 'three' を出力
echo $alt(); // 'four' を出力
echo $alt(); // 'one' を出力
// ~など

tr($string, $array = array())

tr メソッドは、PHP の strtr() を用いて、与えられた文字列からパラメータをパースします。

静的 はい
パラメータ
パラメータ デフォルト 説明
$string string 必須 入力文字列。
$array array
array()
str_replace に渡すパラメータ。
返り値 string
Str::tr('Hello :name', array('name' => 'World')); // 返り値: 'Hello World'

is_json($string)

is_json メソッドは、文字列が json エンコードされているかチェックします。

静的 はい
パラメータ
パラメータ デフォルト 説明
$string string 必須 入力文字列。
返り値 bool
Str::is_json('{"0":"An","encoded":["string"]}'); // 返り値: true

is_serialized($string)

is_serialized メソッドは、文字列が PHP シリアライズされているかチェックします。

静的 はい
パラメータ
パラメータ デフォルト 説明
$string string 必須 入力文字列。
返り値 bool
Str::is_serialized('a:2:{i:0;s:2:"An";s:7:"encoded";a:1:{i:0;s:6:"string";}}');
// 返り値: true

is_html($string)

is_html メソッドは、文字列が HTML かチェックします。

静的 はい
パラメータ
パラメータ デフォルト 説明
$string string 必須 入力文字列。
返り値 bool
Str::is_html('Lorem ipsum <b>dolor sit amet</b>, consectetur <u>adipiscing</u> elit.');
// 返り値: true

is_xml($string)

is_xml メソッドは、文字列が XML かチェックします。 libxml エクステンションが必要です。

静的 はい
パラメータ
パラメータ デフォルト 説明
$string string 必須 入力文字列。
返り値 bool
Str::is_xml('<?xml version="1.0" encoding="utf-8"?><xml><foo>bar</foo></xml>');
// 返り値: true

starts_with($str, $start, $ignore_case = false)

starts_with メソッドは、文字列が指定した文字列で始まっているかチェックします。

静的 はい
パラメータ
パラメータ デフォルト 説明
$str 必須 チェック対象の文字列。
$start 必須 この文字列で始まっているか。
$ignore_case
false
大文字小文字を無視するかどうか。
返り値 bool
$string = "Lorem ipsum dolor sit amet";

Str::starts_with($string, 'Lorem'); // 返り値: true
Str::starts_with($string, 'lorem'); // 返り値: false
Str::starts_with($string, 'lorem', true); // 返り値: true

ends_with($str, $end, $ignore_case = false)

ends_with メソッドは、文字列が指定した文字列で終わっているかチェックします。

静的 はい
パラメータ
パラメータ デフォルト 説明
$str 必須 チェック対象の文字列。
$end 必須 この文字列で終わっているか。
$ignore_case
false
大文字小文字を無視するかどうか。
返り値 bool
$string = "Lorem ipsum dolor sit amet";

Str::ends_with($string, 'amet'); // 返り値: true
Str::ends_with($string, 'Amet'); // 返り値: false
Str::ends_with($string, 'Amet', true); // 返り値: true