辅助函数 字符串函数
Laravel 辅助函数 字符串
__()
__
函数会使用本地化文件翻译给定翻译字符串或翻译键:
echo __('Welcome to our application');
echo __('messages.welcome');
如果给定翻译字符串或键不存在,__
函数将会返回给定值。所以,使用上面的例子,如果翻译键不存在的话 __
函数将会返回 messages.welcome。
class_basename()
class_basename
返回给定类移除命名空间后的类名:
$class = class_basename('Foo\Bar\Baz');
// Baz
e()
e
函数在给定字符串上运行 htmlentities(double_encode 选项设置为 false):
echo e('<html>foo</html>');
// <html>foo</html>
preg_replace_array()
preg_replace_array
函数使用数组替换字符串序列中的给定模式:
$string = 'The event will take place between :start and :end';
$replaced = preg_replace_array('/:[a-z_]+/', ['8:30', '9:00'], $string);
// The event will take place between 8:30 and 9:00
Str::after()
Str::after
方法返回字符串中给定值之后的所有内容:
use Illuminate\Support\Str;
$slice = Str::after('This is my name', 'This is');
// ' my name'
Str::afterLast()
Str::afterLast
方法会返回字符串中给定值最后一次出现位置之后的所有字符,如果该值在字符串中不存在则返回整个字符串:
use Illuminate\Support\Str;
$slice = Str::afterLast('App\Http\Controllers\Controller', '\\');
// 'Controller'
Str::before()
Str::before
方法返回字符串中给定值之前的所有内容:
use Illuminate\Support\Str;
$slice = Str::before('This is my name', 'my name');
// 'This is '
Str::beforeLast()
Str::beforeLast
方法会返回字符串中给定值最后一次出现位置之前的所有字符:
use Illuminate\Support\Str;
$slice = Str::beforeLast('This is my name', 'is');
// 'This '
Str::camel()
Str::camel
将给定字符串转化为形如 camelCase 的驼峰风格:
use Illuminate\Support\Str;
$converted = Str::camel('foo_bar');
// fooBar
Str::contains()
Str::contains
方法判断给定字符串是否包含给定值(大小写敏感):
use Illuminate\Support\Str;
$contains = Str::contains('This is my name', 'my');
// true
还可以传递数组值判断给定字符串是否包含数组中的任意值:
use Illuminate\Support\Str;
$contains = Str::contains('This is my name', ['my', 'foo']);
// true
Str::containsAll()
Str::containsAll
方法用于判断给定字符串是否包含所有数组值:
use Illuminate\Support\Str;
$containsAll = Str::containsAll('This is my name', ['my', 'name']);
// true
Str::endsWith()
Str::endsWith
方法用于判断字符串是否以给定值结尾:
use Illuminate\Support\Str;
$result = Str::endsWith('This is my name', 'name');
// true
你还可以传递数组来判断给定字符串是否以数组中的任意值作为结尾:
use Illuminate\Support\Str;
$result = Str::endsWith('This is my name', ['name', 'foo']);
// true
$result = Str::endsWith('This is my name', ['this', 'foo']);
// false
Str::finish()
Str::finish
方法添加给定值单个实例到字符串结尾 —— 如果原字符串不以给定值结尾的话:
use Illuminate\Support\Str;
$adjusted = Str::finish('this/string', '/');
// this/string/
$adjusted = Str::finish('this/string/', '/');
// this/string/
Str::is()
Str::is
方法判断给定字符串是否与给定模式匹配,星号可用于表示通配符:
use Illuminate\Support\Str;
$matches = Str::is('foo*', 'foobar');
// true
$matches = Str::is('baz*', 'foobar');
// false
Str::ucfirst()
Str::ucfirst
方法会以首字母大写格式返回给定字符串:
use Illuminate\Support\Str;
$string = Str::ucfirst('foo bar');
// Foo bar
Str::isUuid()
Str::isUuid
方法会判断给定字符串是否是有效的 UUID:
use Illuminate\Support\Str;
$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');
// true
$isUuid = Str::isUuid('laravel');
// false
Str::kebab()
Str::kebeb
方法将给定字符串转化为形如 kebab-case 风格的字符串:
use Illuminate\Support\Str;
$converted = Str::kebab('fooBar');
// foo-bar
Str::limit()
Str::limit
方法以指定长度截断字符串:
use Illuminate\Support\Str;
$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20);
// The quick brown fox...
还可以传递第三个参数来改变字符串末尾字符:
use Illuminate\Support\Str;
$truncated = Str::limit('The quick brown fox jumps over the lazy dog', 20, ' (...)');
// The quick brown fox (...)
Str::orderedUuid()
Str::orderedUuid
方法会生成一个「时间戳优先」 的 UUID 以便更高效地存储到数据库索引字段:
use Illuminate\Support\Str;
return (string) Str::orderedUuid();
Str::plural()
Str::plural
方法将字符串转化为复数形式,该函数当前只支持英文:
use Illuminate\Support\Str;
$plural = Str::plural('car');
// cars
$plural = Str::plural('child');
// children
还可以传递整型数据作为第二个参数到该函数以获取字符串的单数或复数形式:
use Illuminate\Support\Str;
$plural = Str::plural('child', 2);
// children
$plural = Str::plural('child', 1);
// child
Str::random()
Str::random
方法通过指定长度生成随机字符串,该函数使用了PHP的 random_bytes 函数:
use Illuminate\Support\Str;
$random = Str::random(40);
Str::replaceArray()
Str::replaceArray
方法使用数组在字符串序列中替换给定值:
use Illuminate\Support\Str;
$string = 'The event will take place between ? and ?';
$replaced = Str::replaceArray('?', ['8:30', '9:00'], $string);
// The event will take place between 8:30 and 9:00
Str::replaceFirst()
Str::replaceFirst
方法会替换字符串中第一次出现的值:
use Illuminate\Support\Str;
$replaced = Str::replaceFirst('the', 'a', 'the quick brown fox jumps over the lazy dog');
// a quick brown fox jumps over the lazy dog
Str::replaceLast()
Str::replaceLast
方法会替换字符串中最后一次出现的值:
use Illuminate\Support\Str;
$replaced = Str::replaceLast('the', 'a', 'the quick brown fox jumps over the lazy dog');
// the quick brown fox jumps over a lazy dog
Str::singular()
Str::singular
方法将字符串转化为单数形式,该函数目前只支持英文:
use Illuminate\Support\Str;
$singular = Str::singular('cars');
// car
$singular = Str::singular('children');
// child
Str::slug()
Str::slug
方法将给定字符串生成 URL 友好的格式:
use Illuminate\Support\Str;
$slug = Str::slug('Laravel 5 Framework', '-');
// laravel-5-framework
Str::snake()
Str::snake
方法将给定字符串转换为形如 snake_case 格式字符串:
use Illuminate\Support\Str;
$converted = Str::snake('fooBar');
// foo_bar
Str::start()
如果字符串没有以给定值开头的话 Str::start 方法会将给定值添加到字符串最前面:
use Illuminate\Support\Str;
$adjusted = Str::start('this/string', '/');
// /this/string
$adjusted = Str::start('/this/string', '/');
// /this/string
Str::startsWith()
Str::startsWith
方法用于判断传入字符串是否以给定值开头:
use Illuminate\Support\Str;
$result = Str::startsWith('This is my name', 'This');
// true
Str::studly()
Str::studly
方法将给定字符串转化为形如 StudlyCase 的、单词开头字母大写的格式:
use Illuminate\Support\Str;
$converted = Str::studly('foo_bar');
// FooBar
Str::title()
Str::title
方法将字符串转化为形如 Title Case 的格式:
use Illuminate\Support\Str;
$converted = Str::title('a nice title uses the correct case');
// A Nice Title Uses The Correct Case
Str::uuid()
Str::uuid
方法生成一个 UUID(版本4):
use Illuminate\Support\Str;
return (string) Str::uuid();
Str::words()
Str::words
方法会限制字符串单词个数:
use Illuminate\Support\Str;
return Str::words('Perfectly balanced, as all things should be.', 3, ' >>>');
// Perfectly balanced, as >>>
trans()
trans
函数使用本地文件转换指定的键名对应的文本:
echo trans('messages.welcome');
如果指定键不存在,trans 函数会返回指定的键名,所以,以上面的示例为例,如果指定的键不存在,trans 函数会返回 messages.welcome。
trans_choice()
trans_choice
函数翻译带拐点的给定翻译键:
echo trans_choice('messages.notifications', $unreadCount);
如果指定的翻译键不存在,trans_choice 函数会将其返回。所以,以上面的示例为例,如果指定翻译键不存在 trans_choice 函数会返回 messages.notifications。