字符串

    引入相关类

    • use Leevel\Support\Str;

    随机小写字母和数字

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

    ::: 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. 1 === preg_match('/^[A-Za-z]+$/', Str::randAlpha(4, 'ABCDEFGefijk'))
    8. );
    9. $this->assertTrue(
    10. 4 === strlen(Str::randAlpha(4))
    11. );
    12. }

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

    随机小写字母

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

    随机数字

    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(5, ''));
    4. $this->assertTrue(
    5. 4 === strlen(Str::randStr(4, 'helloworld'))
    6. }

    日期格式化

    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 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. }