字符串

这里为系统提供的字符串使用的功能文档说明。

引入相关类

  • use Leevel\Support\Str;

    随机字母数字

  1. public function testBaseUse()
  2. {
  3. $this->assertSame('', Str::randAlphaNum(0));
  4. $this->assertTrue(
  5. 1 === preg_match('/^[A-Za-z0-9]+$/', Str::randAlphaNum(4))
  6. );
  7. $this->assertTrue(
  8. 1 === preg_match('/^[A-Za-z0-9]+$/', Str::randAlphaNum(4, 'AB4cdef'))
  9. );
  10. $this->assertTrue(
  11. 4 === strlen(Str::randAlphaNum(4))
  12. );
  13. }

随机小写字母和数字

利用本方法可以生成随机数小写字母。

  1. public function testRandAlphaNumLowercase()
  2. {
  3. $this->assertSame('', Str::randAlphaNumLowercase(0));
  4. $this->assertTrue(
  5. 1 === preg_match('/^[a-z0-9]+$/', Str::randAlphaNumLowercase(4))
  6. );
  7. $this->assertTrue(
  8. 1 === preg_match('/^[a-z0-9]+$/', Str::randAlphaNumLowercase(4, 'cdefghigk2450'))
  9. );
  10. $this->assertTrue(
  11. 4 === strlen(Str::randAlphaNumLowercase(4))
  12. );
  13. }

::: tip
支持位数和指定字符范围
:::

随机大写字母和数字

利用本方法可以生成随机数大写字母。

  1. public function testRandAlphaNumUppercase()
  2. {
  3. $this->assertSame('', Str::randAlphaNumUppercase(0));
  4. $this->assertTrue(
  5. 1 === preg_match('/^[A-Z0-9]+$/', Str::randAlphaNumUppercase(4))
  6. );
  7. $this->assertTrue(
  8. 1 === preg_match('/^[A-Z0-9]+$/', Str::randAlphaNumUppercase(4, 'ABCDEFG1245'))
  9. );
  10. $this->assertTrue(
  11. 4 === strlen(Str::randAlphaNumUppercase(4))
  12. );
  13. }

::: tip
支持位数和指定字符范围
:::

随机字母

利用本方法可以生成随机字母。

  1. public function testRandAlpha()
  2. {
  3. $this->assertSame('', Str::randAlpha(0));
  4. $this->assertTrue(
  5. 1 === preg_match('/^[A-Za-z]+$/', Str::randAlpha(4))
  6. );
  7. $this->assertTrue(
  8. 1 === preg_match('/^[A-Za-z]+$/', Str::randAlpha(4, 'ABCDEFGefijk'))
  9. );
  10. $this->assertTrue(
  11. 4 === strlen(Str::randAlpha(4))
  12. );
  13. }

::: tip
支持位数和指定字符范围
:::

随机小写字母

  1. public function testRandAlphaLowercase()
  2. {
  3. $this->assertSame('', Str::randAlphaLowercase(0));
  4. $this->assertTrue(
  5. 1 === preg_match('/^[a-z]+$/', Str::randAlphaLowercase(4))
  6. );
  7. $this->assertTrue(
  8. 1 === preg_match('/^[a-z]+$/', Str::randAlphaLowercase(4, 'cdefghigk'))
  9. );
  10. $this->assertTrue(
  11. 4 === strlen(Str::randAlphaLowercase(4))
  12. );
  13. }

随机大写字母

  1. public function testRandAlphaUppercase()
  2. {
  3. $this->assertSame('', Str::randAlphaUppercase(0));
  4. $this->assertTrue(
  5. 1 === preg_match('/^[A-Z]+$/', Str::randAlphaUppercase(4))
  6. );
  7. $this->assertTrue(
  8. 1 === preg_match('/^[A-Z]+$/', Str::randAlphaUppercase(4, 'ABCDEFG'))
  9. );
  10. $this->assertTrue(
  11. 4 === strlen(Str::randAlphaUppercase(4))
  12. );
  13. }

随机数字

  1. public function testRandNum()
  2. {
  3. $this->assertSame('', Str::randNum(0));
  4. $this->assertTrue(
  5. 1 === preg_match('/^[0-9]+$/', Str::randNum(4))
  6. );
  7. $this->assertTrue(
  8. 1 === preg_match('/^[0-9]+$/', Str::randNum(4, '012456'))
  9. );
  10. $this->assertTrue(
  11. 4 === strlen(Str::randNum(4))
  12. );
  13. }

随机字中文

  1. public function testRandChinese()
  2. {
  3. $this->assertSame('', Str::randChinese(0));
  4. $this->assertTrue(
  5. 1 === preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', Str::randChinese(4))
  6. );
  7. $this->assertTrue(
  8. 1 === preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', Str::randChinese(4, '我是一个中国人'))
  9. );
  10. $this->assertTrue(
  11. 12 === strlen(Str::randChinese(4))
  12. );
  13. }

随机字符串

  1. public function testRandStr()
  2. {
  3. $this->assertSame('', Str::randStr(0, ''));
  4. $this->assertSame('', Str::randStr(5, ''));
  5. $this->assertTrue(
  6. 4 === strlen(Str::randStr(4, 'helloworld'))
  7. );
  8. }

日期格式化

  1. public function testFormatDate()
  2. {
  3. $time = time();
  4. $this->assertSame(date('Y-m-d H:i', $time + 600), Str::formatDate($time + 600));
  5. $this->assertSame(date('Y-m-d', $time + 600), Str::formatDate($time + 600, [], 'Y-m-d'));
  6. $this->assertSame(date('Y-m-d', $time - 1286400), Str::formatDate($time - 1286400, [], 'Y-m-d'));
  7. $this->assertSame('1 hours ago', Str::formatDate($time - 5040));
  8. $this->assertSame('1 小时之前', Str::formatDate($time - 5040, ['hours' => '小时之前']));
  9. $this->assertSame('4 minutes ago', Str::formatDate($time - 240));
  10. $this->assertSame('4 分钟之前', Str::formatDate($time - 240, ['minutes' => '分钟之前']));
  11. $this->assertTrue(in_array(Str::formatDate($time - 40), ['40 seconds ago', '41 seconds ago', '42 seconds ago'], true));
  12. $this->assertTrue(in_array(Str::formatDate($time - 40, ['seconds' => '秒之前']), ['40 秒之前', '41 秒之前', '42 秒之前'], true));
  13. }

文件大小格式化

  1. public function testFormatBytes()
  2. {
  3. $this->assertSame('2.4G', Str::formatBytes(2573741824));
  4. $this->assertSame('2.4', Str::formatBytes(2573741824, false));
  5. $this->assertSame('4.81M', Str::formatBytes(5048576));
  6. $this->assertSame('4.81', Str::formatBytes(5048576, false));
  7. $this->assertSame('2.95K', Str::formatBytes(3024));
  8. $this->assertSame('2.95', Str::formatBytes(3024, false));
  9. $this->assertSame('100B', Str::formatBytes(100));
  10. $this->assertSame('100', Str::formatBytes(100, false));
  11. }

下划线转驼峰

  1. public function testCamelize()
  2. {
  3. $this->assertSame('helloWorld', Str::camelize('helloWorld'));
  4. $this->assertSame('helloWorld', Str::camelize('helloWorld', '-'));
  5. $this->assertSame('helloWorld', Str::camelize('hello_world'));
  6. $this->assertSame('helloWorld', Str::camelize('hello-world', '-'));
  7. }

驼峰转下划线

  1. public function testUnCamelize()
  2. {
  3. $this->assertSame('hello_world', Str::unCamelize('hello_world'));
  4. $this->assertSame('hello-world', Str::unCamelize('hello-world', '-'));
  5. $this->assertSame('hello_world', Str::unCamelize('helloWorld'));
  6. $this->assertSame('hello-world', Str::unCamelize('helloWorld', '-'));
  7. }

判断字符串中是否包含给定的字符开始

  1. public function testStartsWith()
  2. {
  3. $this->assertFalse(Str::startsWith('foo', 'hello'));
  4. $this->assertTrue(Str::startsWith('foo bar', 'foo'));
  5. }

判断字符串中是否包含给定的字符结尾

  1. public function testEndsWith()
  2. {
  3. $this->assertFalse(Str::endsWith('foo', 'hello'));
  4. $this->assertTrue(Str::endsWith('foo bar', 'bar'));
  5. }

判断字符串中是否包含给定的字符串集合

  1. public function testContains()
  2. {
  3. $this->assertFalse(Str::contains('foo', ''));
  4. $this->assertTrue(Str::contains('foo bar', 'foo'));
  5. }