当前位置: 首页 > 文档资料 > FuelPHP 中文文档 >

Str - 类別

优质
小牛编辑
119浏览
2023-12-01

Str 类别是一系列帮助操作字串的方法。

increment($str, $first = 1)

increment 方法能让你附加一个数字字串的结尾或递增已经存在的数字。

静态
参数
参数预设描述
$string必要要递增的字串。
$first
1
要递增的第一个数字。
回传字串
範例
$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 方法基于所给的类型产生一个随机的字串。

静态
参数
参数预设描述
$typealnum要产生的字串类型。你的选择有 alnum、numeric、nozero、alpha、distinct、hexdec、unique、sha1 与 uuid。
$length
16
你想要最后字串的字元长度(unique、sha1 与 uuid 忽略此参数)。
回传字串
範例
// 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(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 = "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)

回传一个闭包(closure),它将会轮替你传给此方法做为变数的值, 除非你使用 false 变数呼叫闭包(closure) - 在此情况下, 将会在下次呼叫时回传相同的值,而没有移动到下一个值。

静态
参数
参数预设描述
*$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'
// 等等……

upper($str, $encoding = null)

upper 方法转换所有字元为大写,它等同于 PHP 的 strtoupper() 可使用指定的字元编码。

静态
参数
参数类型预设描述
$string字串必要输入的字串。
$encoding字串
null
字元编码。
回传字串
範例
Str::upper('User Data'); // 回传 'USER DATA'

lower($str, $encoding = null)

lower 方法转换所有字元为小写,它等同于 PHP 的 strtolower() 可使用指定的字元编码。

静态
参数
参数类型预设描述
$string字串必要输入的字串。
$encoding字串
null
字元编码。
回传字串
範例
Str::lower('User Data'); // 回传 'user data'

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

tr 方法使用 PHP 的 strtr() 从所给的字串解析变数。

静态
参数
参数类型预设描述
$string字串必要输入的字串。
$array阵列
array()
要 str_replace 的参数。
回传字串
範例
Str::tr('Hello :name', array('name' => 'World')); // 回传 'Hello World'

is_json($string)

is_json 方法检查一个字串是否为 json 编码。

静态
参数
参数类型预设描述
$string字串必要输入的字串。
回传布林
範例
Str::is_json('{"0":"An","encoded":["string"]}'); // 回传 true

is_serialized($string)

is_serialized 方法检查一个字串是否序列化。

静态
参数
参数类型预设描述
$string字串必要输入的字串。
回传布林
範例
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字串必要输入的字串。
回传布林
範例
Str::is_html('Lorem ipsum <b>dolor sit amet</b>, consectetur <u>adipiscing</u> elit.');
// 回传 true

is_xml($string)

is_xml 方法检查一个字串是否是有效的 xml。需要 libxml 扩充

静态
参数
参数类型预设描述
$string字串必要输入的字串。
回传布林
範例
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
是否要忽略大小写。
回传布林
範例
$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
是否要忽略大小写。
回传布林
範例
$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

sub($str, $start, $length = null, $encoding = null)

sub 方法回传字串指定的一部分,它等同于 PHP 的 substr() 可使用指定的字符集编码。

静态
参数
参数预设描述
$str必要输入的字串。
$start必要第一个字元的定位,详细用法请看 php.net
$length
null
字元的最大值,详细用法请看 php.net
$encoding
null
字符集编码。
回传字串
範例
Str::sub('User Data', 3, -1); // 回传 'r Dat'