torugo/tstring

用于验证和操作字符串的工具集。

1.2.5 2024-09-10 19:03 UTC

This package is auto-updated.

Last update: 2024-09-10 19:04:24 UTC


README

用于验证和处理字符串的小型工具集。

灵感来源于 validator.js

目录

需求

安装

composer require torugo/tstring

使用

您可以通过两种方式使用此库:使用特定的 特质 或实例化 TString 类。

通过特质

特质是仅使用所需函数的好方法,只需在您的类中使用它们即可。

注意

所有验证器和操作特质函数的可见性都是 protected static

use Torugo\TString\Traits\Validators\TStringContains;

class MyClass()
{
    use TStringContains;

    public function myValidation() {
        // ...
        if (self::contains($haystack, $needle, false)) {
            // Do something...
        }
        // ...
    }
}

通过 TString 静态对象

TString 类是使用此库函数的最简单方法,通过使用它,您将能够访问单个对象中的所有功能。

use Torugo\TString\TString;

if  (TString::contains($haystack, $needle, false)) {
    // Do something
}

验证器

contains

检查子字符串是否包含在另一个字符串中。

contains(string $haystack, string $needle, bool $caseSensitive = true): bool

注意

默认情况下,启用大小写敏感。

特质命名空间

use Torugo\TString\Traits\Validators\TStringContains;

示例

$text = 'The quick brown fox jumps over the lazy dog';

contains($text, 'fox jumps'); // returns true
contains($text, 'OVER', false); // returns true, case insensitive

contains($text, 'red fox'); // returns false
contains($text, 'LAZY DOG'); // returns false, case sensitive

isAlpha

验证字符串是否只包含字母字符。

isAlpha(string $str, bool $includeUnicode = false): bool

注意

当启用时,参数 $includeUnicode 将包含一些Unicode字母字符,例如带重音的字母和一些语言的字母字符。此选项默认禁用。

特质命名空间

use Torugo\TString\Traits\Validators\TStringIsAlpha;

示例

isAlpha("abcdefghiklmnopqrstvxyzABCDEFGHIKLMNOPQRSTVXYZ"); // returns true
isAlpha("ãáàâäçéêëíïõóôöúüÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ", true); // returns true (unicode enabled)
isAlpha("ανάπτυξη", true); // returns true (unicode enabled)
isAlpha("発達", true); // returns true (unicode enabled)

isAlpha("Some text"); // returns false (no spaces)
isAlpha("ãáàâäçéêëíïõóôöúüÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ"); // returns false (unicode disabled)
isAlpha("지능"); // returns false (unicode disabled)
isAlpha("upplýsingaöflun"); // returns false (unicode disabled)

isAlphanumeric

验证字符串是否只包含字母数字字符。

isAlphanumeric(string $str, bool $includeUnicode = false): bool

注意

当启用时,参数 $includeUnicode 将包含一些Unicode字母字符,例如带重音的字母和一些语言的字母字符。此选项默认禁用。

特质命名空间

use Torugo\TString\Traits\Validators\TStringIsAlphanumeric;

示例

isAlphanumeric("abcdefghiklmnopqrstvxyzABCDEFGHIKLMNOPQRSTVXYZ0123456789"); // returns true
isAlphanumeric("twy5Z0V0lzhOItTa"); // returns true
isAlphanumeric("iZmáüàyÍsúL6DI00à0äúPÏvy", true); // returns true (unicode enabled)
isAlphanumeric("έτος2024", true); // returns true (unicode enabled)
isAlphanumeric("1983年は最高の年だ", true); // returns true (unicode enabled)

isAlphanumeric("march 1983"); // returns false
isAlphanumeric("13/03/1983"); // returns false
isAlphanumeric("έτος2024"); // returns false (unicode disabled)
isAlphanumeric("1983年は最高の年だ"); // returns false (unicode disabled)

isBase64

验证字符串是否为Base64格式。
也验证URL安全的Base64字符串。

protected static function isBase64(string $base64): bool;

特质命名空间

use Torugo\TString\Traits\Validators\TStringIsBase64;

示例

isBase64('THVrZSBJIGFtIHlvdXIgZmF0aGVyIQ=='); // returns true
isBase64('V2h5IHNvIHNlcmlvdXM/'); // returns true (url safe)
isBase64('VGhp/cy=BpcyBhIHRlc3Q'); // returns false

isCnpj

验证给定字符串是否具有有效的CNPJ注册。

巴西国家法律实体登记处号码(CNPJ)是一家公司识别号码,必须在开始任何商业活动之前从联邦税务局(Secretaria da Receita Federal do Brasil)获得。

protected static function isCnpj(string $cnpj): bool

特质命名空间

use Torugo\TString\Traits\Validators\TStringIsCnpj;

示例

isCnpj('60391682000132'); // returns true
isCnpj('99.453.669/0001-04'); // returns true, this is the default format
isCnpj('99 453 669 / 0001 (04)'); // returns true, it removes non numerical characters

isCnpj('99.453.669/0001-05'); // returns false, invalid verification digit
isCnpj('9953669000105'); // returns false, invalid length
isCnpj('999.453.669/0001-04'); // returns false, invalid length

注意

此验证器使用来自 Guilherme Sehn 的验证码。

重要

上述CNPJ号码是使用 此工具 随机生成的。
如果其中之一属于您,请向我发送请求以删除。

isCpf

验证给定字符串是否具有有效的CPF身份。

CPF代表“Cadastro de Pessoas Físicas”或“个人登记册”。它类似于美国采用的“社会保障”号码,并在巴西用作一种通用标识符。

protected static function isCpf(string $cpf): bool

特质命名空间

use Torugo\TString\Traits\Validators\TStringIsCpf;

示例

isCpf('88479747048'); // returns true
isCpf('532.625.750-54'); // returns true, this is the default format
isCpf('532 625 750 (54)'); // returns true, removes non numerical characters

isCpf('532.625.750-55'); // returns false, invalid verification digit
isCpf('53.625.750-54'); // returns false, invalid length
isCpf('532.625.750-541'); // returns false, invalid length

注意

此验证器使用来自 Rafael Neri 的验证码。

重要

上述CPF号码是使用 此工具 随机生成的。
如果其中之一属于您,请发送请求,我将立即删除。

IsEmail

验证字符串是否具有有效的电子邮件结构。

isEmail(string $email): bool

特质命名空间

use Torugo\TString\Traits\Validators\TStringIsEmail;

示例

// RETURNS TRUE
isEmail('foo@bar.com');
isEmail('x@x.com');
isEmail('foo@bar.com.br');
isEmail('foo+bar@bar.com');


// RETURNS FALSE
isEmail('invalidemail@');
isEmail('invalid.com');
isEmail('@invalid.com');
isEmail('foo@bar.com.');

提示

查看 测试 以查看更多有效或无效的电子邮件。

IsHexadecimal

验证字符串是否为十六进制数。

isHexadecimal(string $hex): bool

特质命名空间

use Torugo\TString\Traits\Validators\TStringIsHexadecimal;

示例

isHexadecimal('c0627d4e8eae2e8e584d'); // returns true
isHexadecimal('1D5D98'); // returns true
isHexadecimal('0x4041E2F71BA5'); // returns true
isHexadecimal('0x15e1aea12b49'); // returns true

isHexadecimal('i0qh9o2pfm'); // returns false
isHexadecimal('#4EFCB7'); // returns false
isHexadecimal(' 4EFCB7 '); // returns false
isHexadecimal(''); // returns false
isHexadecimal('0X62F12E'); // returns true

isLength

验证字符串长度是否在最小和最大参数之间。

isLength(string $str, int $min, int $max): bool

重要

如果$min为负,则将其设置为0(零)。
如果$max小于1,则将其设置为1
如果$min小于$max,则它们的值将互换。

特质命名空间

use Torugo\TString\Traits\Validators\TStringIsNumeric;

示例

isLength('MySuperStrongPassword!', 8, 64); // returns true
isLength('yágftÔúÍézÏP5mÕ3(8G}KQÖÜ', 24, 26); // returns true

isLength('fZ?ávãYów3j);ÜMK7!:k', 10, 20); // returns false, exceeded maximum length
isLength('xRh8É<', 8, 16); // returns false, did not reach the minimum length

isNumeric

验证字符串是否只包含数字字符。

public static function isNumeric(string $str, bool $includePonctuation = false): bool

特质命名空间

use Torugo\TString\Traits\Validators\TStringIsNumeric;

示例

isNumeric('100'); // returns true
isNumeric('-15'); // returns true 
isNumeric('3.1415', true); // returns true, ponctuation enabled
isNumeric('1,999.99', true); // returns true, ponctuation enabled

isNumeric('3.1415'); // returns false, ponctuation disabled
isNumeric('R$ 999,99', true); // returns false, invalid characters
isNumeric('2.2.0', true); // returns false

isSemVer

public static function isSemVer(string $version): bool

特质命名空间

use Torugo\TString\Traits\Validators\TStringIsSemVer;

示例

// VALID
isSemVer('0.0.4');
isSemVer('1.2.3');
isSemVer('10.20.30');
isSemVer('1.0.0-alpha');
isSemVer('1.1.0-beta');
isSemVer('1.1.1-rc');
isSemVer('1.1.1+72');

// INVALID
isSemVer('1');
isSemVer('1.0');
isSemVer('alpha.beta');
isSemVer('v1.0.0');
isSemVer('01.1.1');
isSemVer('1.2.3.beta');

isUrl

验证字符串是否只包含数字字符。

注意

此验证器基于validator.js

public static function isUrl(string $url, URLOptions $options = false): bool

特质命名空间

use Torugo\TString\Traits\Validators\TStringIsUrl;

示例

// VALID
isUrl('foobar.com');
isUrl('www.foobar.com');
isUrl('http://www.foobar.com/');
isUrl('http://127.0.0.1/',);
isUrl('http://10.0.0.0/',);
isUrl('http://189.123.14.13/',);
isUrl('http://duckduckgo.com/?q=%2F',);

// INVALID
isUrl('http://www.foobar.com:0/',);
isUrl('http://www.foobar.com:70000/',);
isUrl('http://www.foobar.com:99999/',);
isUrl('http://www.-foobar.com/',);
isUrl('http://www.foobar-.com/',);

UrlOptions

默认值

new UrlOptions(
    requireTld: true,
    requireProtocol: false, // expects the protocol to be present in the url
    requireValidProtocol: true, // requires one of the protocols bellow
    protocols: ["http", "https", "ftp"], // required protocols
    requireHost: true,
    requirePort: false,
    allowUnderscores: false,
    allowTrailingDot: false,
    allowProtocolRelativeUrls: false,
    allowFragments: true,
    allowQueryComponents: true,
    allowAuth: true,
    allowNumericTld: false,
    allowWildcard: false,
    validateLength: true,
);

maxLength

验证字符串长度是否小于或等于最大参数。

maxLength(string $str, int $max): bool

特质命名空间

use Torugo\TString\Traits\Validators\TStringMaxLength;

示例

maxLength('pSqKDfErCG5zTkmh', 18); // returns true
maxLength('cETíÁ4ÃR9k=Hj7óGÜt@8', 20); // returns true

maxLength('DXaEbx', 5); // returns false

maxLength('X', 0); // sets max parameter to 1 and returns true
maxLength('Y', -1); // sets max parameter to 1 and returns true

minLength

验证字符串长度是否大于或等于最小参数。

minLength(string $str, int $min): bool

特质命名空间

use Torugo\TString\Traits\Validators\TStringMinLength;

示例

minLength('kfRb7qhmdWear4X9', 15); // returns true
minLength('jCa3xMe9GZ82pmKu', 16); // returns true

minLength('afdvkxzeg9AwrB8D57XE3pj', 24); // returns false

minLength('Y', -1); // sets min parameter to 0 and returns true

maxVersion

检查版本号是否小于或等于给定版本。

此函数验证只有数字通过点分隔的版本号。

  • 有效: '8.3.8', '5.0', '3.22.2', '10', '126.0.6478.63' ...
  • 无效: '2.0.0-rc.1', '1.0.0-beta' ...
maxVersion(string $version, string $maxVersion): bool

特质命名空间

use Torugo\TString\Traits\Validators\TStringMaxVersion;

示例

maxVersion('1.0.0', '1.0.1') // returns true
maxVersion('2.0.0', '2.1') // returns true
maxVersion('3.0.0', '3.0.1') // returns true
maxVersion('3.2.4', '3.2.5') // returns true

maxVersion('1.0.1', '1.0.0') // returns false
maxVersion('2.2.0', '2.1.0') // returns false
maxVersion('1.1', '1') // returns false

minVersion

检查版本是否大于或等于给定版本。

此函数验证只有数字通过点分隔的版本号。

  • 有效: '8.3.8', '5.0', '3.22.2', '10', '126.0.6478.63' ...
  • 无效: '2.0.0-rc.1', '1.0.0-beta' ...
minVersion(string $version, string $required): bool

特质命名空间

use Torugo\TString\Traits\Validators\TStringMinVersion;

示例

minVersion('1.0.0', '1.0.0') // returns true
minVersion('2.1', '2.0.0') // returns true
minVersion('1.0.9', '1') // returns true
minVersion('3.2.5', '3.2.5') // returns true

minVersion('1.0.0', '1.0.1') // returns false
minVersion('2.1.0', '2.2.0') // returns false
minVersion('1', '1.1') // returns false

处理器

toString

尝试将某些类型转换为字符串。

toString(mixed $value, string $arraySeparator = ''): string|false

注意

成功时返回一个string,失败时返回false
$arraySeparator仅在值类型为'array'时使用。

特质命名空间

use Torugo\TString\Traits\Handlers\TStringToString;

示例

toString(2017); // returns '2017'
toString(1999.99); // returns '1999.99'
toString(true); // returns 'true'
toString(["A", "B", "C"]); // returns 'ABC'
toString([185, 85, 0, 29], '.'); // returns '185.85.0.29'

toLowerCase

将字符串转换为小写。

toLowerCase(string $str): string

特质命名空间

use Torugo\TString\Traits\Handlers\TStringToLowerCase;

示例

toLowerCase('LUKE I AM YOUR FATHER'); // returns 'luke i am your father'
toLowerCase('R2D2'); // returns 'r2d2'
toLowerCase('JOSÉ DE ALENCAR'); // returns 'josé de alencar'

toTitleCase

将字符串转换为标题大小写,带有修复罗马数字和葡萄牙语介词的选项。

toTitleCase(
    string $str,
    bool $fixRomanNumerals = false,
    bool $fixPortuguesePrepositions = false
): string

注意

默认禁用$fixRomanNumerals$fixPortuguesePrepositions参数。

特质命名空间

use Torugo\TString\Traits\Handlers\TStringToTitleCase;

示例

// WITH DEFAULT OPTIONS
toTitleCase('LUKE SKYWALKER'); // returns 'Luke Skywalker'
toTitleCase('carlos drummond de andrade'); // returns 'Carlos Drummond De Andrade'
toTitleCase('Pope Gregory XIII'); // returns 'Pope Gregory Xiii'

// FIXING ROMAN NUMERALS
toTitleCase('pope gregory xiii', true, false); // returns 'Pope Gregory XIII'
toTitleCase('DALAI LAMA XIV', true, false); // returns 'Dalai Lama XIV'

// FIXING PORTUGUESE PREPOSITIONS
toTitleCase('NISE DA SILVEIRA', false, true); // returns 'Nise da Silveira'
toTitleCase('Tarsila Do Amaral', false, true); // returns 'Tarsila do Amaral'

// BOTH OPTIONS ENABLED
toTitleCase('xv de piracicaba', true, true); // returns 'XV de Piracicaba'
toTitleCase('JOSÉ BONIFÁCIO DE ANDRADA E SILVA II', true, true); // returns 'José Bonifácio de Andrada e Silva II'

toUpperCase

将字符串转换为大写。

toUpperCase(string $str): string

特质命名空间

use Torugo\TString\Traits\Handlers\TStringToUpperCase;

示例

toUpperCase('may the force be with you'); // returns 'MAY THE FORCE BE WITH YOU'
toUpperCase('c3po'); // returns 'C3PO'
toUpperCase('Cecília Meireles'); // returns 'CECÍLIA MEIRELES'

贡献

目前不接受贡献,我打算尽快使其可用。

许可协议

此库采用MIT许可证 - 有关详细信息,请参阅LICENSE文件。