/php-string-helpers

some useful php string helpers

Primary LanguagePHPMIT LicenseMIT

Helpful Set Of Php String Helper Functions Utilities Class

License: MIT Latest Version on Packagist Total Downloads

All function helpers will be enabled by default (if those functions haven't already been defined). also you can use them as utilities class.

Requirements

  • PHP 8.1 or higher

Installation

You can install the package via composer:

composer require dvlpr1996/php-string-helpers

StrUtility usage as static methods

you can use String helper methods as static so usage like the following: First Using The StrUtility Class:

use dvlpr1996\PhpStringHelpers\Facade\StrUtility;

  • change words to camel case
StrUtility::toCamelCase(string $words): string
  • change words to pascal case
StrUtility::toPascalCase(string $words): string
  • change words to kebab case
StrUtility::toKebabCase(string $words): string
  • change words to title case
StrUtility::toTitleCase(string $words): string
  • change words to constant case | foo bar baz change to FOO_BAR_BAZ
StrUtility::toConstant(string $words): string
  • change words to snake case
StrUtility::toSnakeCase(string $words): string  
  • change words to path case | foo bar baz change to foo/bar/baz
StrUtility::toPathCase(string $words): string
  • change words to ada case | foo bar change to Foo_Bar
StrUtility::toAdaCase(string $words): string
  • change words to dot notation case | foo bar change to foo.bar
StrUtility::dotNotation(string $words): string  
  • wrapper for htmlspecialchars()
StrUtility::entitiesWrapper($data): string
  • change string to slug
StrUtility::toSlug(string $string): string
  • remove all blanks
StrUtility::rmAllBlanks(string $string): string     
  • return alternate string if string param is null
StrUtility::alternate(?string $string, string $alternate = null): string    
  • translation methods, for using this method you should create a wrapper function for example
function <your_wrapper_function_name>(string $key, string $alternative = '', string $dirName = 'en')
{
    $BASE_PATH = // base (root) path of your project

    $translatePath = StrUtility::translatePath($BASE_PATH, $dirName);
    return StrUtility::translate($translatePath . $key, $alternative);
}

< your_wrapper_function_name>('app.title') reference to lang/en/app.php and title array key in app.php file app.php must only return associative array. this translation methods only work for one level

StrUtility::translate(string $key, string $alternative = ''): string|array
  • wrap given string with wrapper param
StrUtility::wrapper(int|string $string, int|string $wrapper = '*'): string
  • return path of file
StrUtility::filePath(string $path, string $pathExtension = 'php'): string
  • generate unique pin numbers between 4 digit and 12 digit
StrUtility::generatePin(int $length = 4): int
  • clean and safe given string data
StrUtility::clearString(string $data): string
  • return only string and remove other characters
StrUtility::pureString(string $data): string
  • generate random string
StrUtility::randomChar(int $size = 5): string  
  • generate random hexadecimal color
StrUtility::randomHex(): string
  • generate random rgb color
StrUtility::randomRgb(): string
  • Remove all types of links
StrUtility::rmLink(string $string): string
  • limit character based on $length and replace theme with ...
StrUtility::limitChar(string|int $string, int $length): string
  • generate unique id numbers
StrUtility::generateId(string|int $prefix ='',string|int $suffix ='',bool $moreEntropy = false): string   
  • remove all numbers
StrUtility::rmNumbers(string $string): string   
  • remove all characters
StrUtility::rmCharacters(string $string): string
  • remove all extra blanks
StrUtility::rmExtraBlank(string $string): string 
  • convert hex color to rgb color
StrUtility::hexToRgb(string $color): ?string 
  • convert rgb color to hex color
StrUtility::rgbToHex(string $color): ?string
  • generate <a> tag link
StrUtility::generateAnchor(string|int $content, string $href): string
  • return encoding of string . wrapper for mb_detect_encoding()
StrUtility::getEncoding(string $string): string
StrUtility::isUtf8(string|array $string): bool
  • remove duplicate words
StrUtility::rmDuplicateWords(string $string): string
  • remove characters from right side based on $num param
StrUtility::rmRightChar(string $words, int $num): string
  • remove characters from left side based on $num param
StrUtility::rmLeftChar(string $words, int $num): string
  • remove characters from both side based on $num param
StrUtility::rmBothSideChar(string $words, int $num): string
  • find whether the type of a data is json
StrUtility::isJson(mixed $data): bool
  • Checks whether the string contains the specified value or not
StrUtility::isContains(string $string, string $search, bool $caseSensitive = false): bool
  • Checks whether the string starts with the specified value <$search> or not
StrUtility::isStartWith(string $string, string $search, bool $caseSensitive = false): bool
  • return the last word
StrUtility::lastWord(string $string): string
  • return the first word
StrUtility::firstWord(string $string): string
  • return the first number
StrUtility::getFirstNumbers(string $string): string
  • return the last number
StrUtility::getLastNumbers(string $string): string
StrUtility::rmBeginningNumbers(string $string): string
StrUtility::rmEndingNumbers(string $string): string
StrUtility::convertToUtf8(string $string): string|bool
  • incrementing the numbers of the end of the string
StrUtility::incrementBy(string $string, ?string $separator = null): string
  • decrementing the numbers of the end of the string
StrUtility::decrementBy(string $string, ?string $separator = null): string
  • remove last word from given string
StrUtility::rmLastWord(string $string): string
  • remove first word from given string
StrUtility::rmFirstWord(string $string): string
  • find whether the type of a given string is slug
StrUtility::is_slug(string $slug): bool
  • find whether the type of a given ip is valid ipv4
StrUtility::is_ipv4(string $ip): bool
  • find whether the type of a given ip is valid ipv6
StrUtility::is_ipv6(string $ip): bool
  • Deletes the words before the given $search word
StrUtility::after(string $string, string $search): string
  • Deletes the words after the given $search word
StrUtility::before(string $string, string $search): string
StrUtility::hasSpace(string $string): bool
StrUtility::isEmail(string $email): bool
StrUtility::detectCase(string $word): string
StrUtility::isLowerCase(string $word): bool
StrUtility::isUpperCase(string $word): bool
StrUtility::isTitleCase(string $word): bool
StrUtility::isSnakeCase(string $word): bool
StrUtility::validateUserName(string $userName, int $min = 3, int $max = 20): bool
StrUtility::humanFileSize(int $size, string $type = 'KB'): string

StrUtility usage as helper functions

String helper functions are global so usage like the following:

decrementBy(string $string, ?string $separator = null): string

StrUtility usage as object

use PhpStringHelpers\utility\StrUtility;

$string = new StrUtility;

$string->toConstant('foo bar baz');

Changelog

Please see CHANGELOG for more information what has changed recently.

issues

If you discover any issues, please using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.