PHP code example of torugo / tstring

1. Go to this page and download the library: Download torugo/tstring library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

torugo / tstring example snippets


use Torugo\TString\Traits\Validators\TStringContains;

class MyClass()
{
    use TStringContains;

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

use Torugo\TString\TString;

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

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(string $str, bool $

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(string $str, bool $

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)

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

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

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

isEmail(string $email): bool

use Torugo\TString\Traits\Validators\TStringIsEmail;

// RETURNS TRUE
isEmail('[email protected]');
isEmail('[email protected]');
isEmail('[email protected]');
isEmail('[email protected]');


// RETURNS FALSE
isEmail('invalidemail@');
isEmail('invalid.com');
isEmail('@invalid.com');
isEmail('[email protected].');

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(string $str, int $min, int $max): bool

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

public static function isNumeric(string $str, 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

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');

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/',);

new UrlOptions(
    : false, // expects the protocol to be present in the url
    rue,
    alse,
    allowFragments: true,
    allowQueryComponents: true,
    allowAuth: true,
    allowNumericTld: false,
    allowWildcard: false,
    validateLength: true,
);

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(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(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(string $version, string $

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(mixed $value, string $arraySeparator = ''): string|false

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(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(
    string $str,
    bool $fixRomanNumerals = false,
    bool $fixPortuguesePrepositions = false
): string

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(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'