PHP Classes

File: docs/strings.md

Recommend this page to a friend!
  Classes of Eric Sizemore   Utility   docs/strings.md   Download  
File: docs/strings.md
Role: Auxiliary data
Content type: text/markdown
Description:
Class: Utility
Collection of various PHP utility functions
Author: By
Last change: in wrong location
Date: 9 months ago
Size: 8,754 bytes
 

Contents

Class file image Download

Strings

Esi\Utility\Strings

  • getEncoding(): string;
  • setEncoding(string $newEncoding = '', bool $iniUpdate = false): void;
  • title(string $value): string;
  • lower(string $value): string;
  • upper(string $value): string;
  • substr(string $string, int $start, ?int $length = null): string;
  • lcfirst(string $string): string;
  • ucfirst(string $string): string;
  • strcasecmp(string $str1, string $str2): int;
  • beginsWith(string $haystack, string $needle, bool $insensitive = false, bool $multibyte = false): bool;
  • endsWith(string $haystack, string $needle, bool $insensitive = false, bool $multibyte = false): bool;
  • doesContain(string $haystack, string $needle, bool $insensitive = false, bool $multibyte = false): bool;
  • doesNotContain(string $haystack, string $needle, bool $insensitive = false, bool $multibyte = false): bool;
  • length(string $string, bool $binarySafe = false): int;
  • camelCase(string $string): string;
  • ascii(string $value, string $language = 'en'): string;
  • slugify(string $title, string $separator = '-', string $language = 'en'): string;
  • randomBytes(int $length): string;
  • randomString(int $length = 8): string;
  • validEmail(string $email): bool;
  • validJson(string $data): bool;
  • obscureEmail(string $email): string;
  • guid(): string;

getEncoding

Returns current encoding.

use Esi\Utility\Strings;

// Defaults to 'UTF-8'
echo Strings::getEncoding(); // 'UTF-8'

setEncoding

Sets the encoding to use for multibyte-based functions.

use Esi\Utility\Strings;

Strings::setEncoding('UCS-2');

echo Strings::getEncoding(); // 'UCS-2'

title

Convert the given string to title case.

use Esi\Utility\Strings;

echo Strings::title('Mary had A little lamb and She Loved it so'); // 'Mary Had A Little Lamb And She Loved It So'

lower

Convert the given string to lower case.

use Esi\Utility\Strings;

echo Strings::lower('tESt'); // 'test'
echo Strings::lower('TEST'); // 'test'

upper

Convert the given string to upper case.

use Esi\Utility\Strings;

echo Strings::upper('teSt'); // 'TEST'

substr

Returns the portion of string specified by the start and length parameters.

use Esi\Utility\Strings;

echo Strings::substr('abcdef', -1); // 'f'

lcfirst

Convert the first character of a given string to lower case.

use Esi\Utility\Strings;

echo Strings::lcfirst('Test'); // 'test'
echo Strings::lcfirst('TEST'); // 'tEST'

ucfirst

Convert the first character of a given string to upper case.

use Esi\Utility\Strings;

echo Strings::ucfirst('test'); // 'Test'
echo Strings::ucfirst('tEsT'); // 'TEsT'

strcasecmp

Compares multibyte input strings in a binary safe case-insensitive manner.

use Esi\Utility\Strings;

// Returns -1 if string1 is less than string2; 1 if string1 is greater than string2, and 0 if they are equal.
$str1 = 'test';
$str2 = 'Test';

var_dump(Strings::strcasecmp($str1, $str2)); // 0

$str1 = 'tes';
var_dump(Strings::strcasecmp($str1, $str2)); // -1

$str1 = 'testing';
var_dump(Strings::strcasecmp($str1, $str2)); // 1

beginsWith

Determine if a string begins with another string.

use Esi\Utility\Strings;

var_dump(Strings::beginsWith('this is a test', 'this')); // bool(true)
var_dump(Strings::beginsWith('this is a test', 'test')); // bool(false)

var_dump(Strings::beginsWith('THIS IS A TEST', 'this', true)); // bool(true)
var_dump(Strings::beginsWith('THIS IS A TEST', 'test', true)); // bool(false)

var_dump(Strings::beginsWith('THIS IS A TEST', 'this', true, true)); // bool(true)
var_dump(Strings::beginsWith('THIS IS A TEST', 'test', true, true)); // bool(false)

endsWith

Determine if a string ends with another string.

use Esi\Utility\Strings;

var_dump(Strings::endsWith('this is a test', 'test')); // bool(true)
var_dump(Strings::endsWith('this is a test', 'this')); // bool(false)

var_dump(Strings::endsWith('THIS IS A TEST', 'test', true)); // bool(true)
var_dump(Strings::endsWith('THIS IS A TEST', 'this', true)); // bool(false)

var_dump(Strings::endsWith('THIS IS A TEST', 'test', true, true)); // bool(true)
var_dump(Strings::endsWith('THIS IS A TEST', 'this', true, true)); // bool(false)

doesContain

Determine if a string exists within another string.

use Esi\Utility\Strings;


var_dump(Strings::doesContain('start a string', 'a string')); // bool(true)
var_dump(Strings::doesContain('start a string', 'starting')); // bool(false)

var_dump(Strings::doesContain('START A STRING', 'a string', true)); // bool(true)
var_dump(Strings::doesContain('START A STRING', 'starting', true)); // bool(false)

var_dump(Strings::doesContain('START A STRING', 'a string', true, true)); // bool(true)
var_dump(Strings::doesContain('START A STRING', 'starting', true, true)); // bool(false)

doesNotContain

Determine if a string does not exist within another string.

use Esi\Utility\Strings;

var_dump(Strings::doesNotContain('start a string', 'stringly')); // bool(true)
var_dump(Strings::doesNotContain('start a string', 'string')); // bool(false)

var_dump(Strings::doesNotContain('START A STRING', 'stringly', true)); // bool(true)
var_dump(Strings::doesNotContain('START A STRING', 'string', true)); // bool(false)

var_dump(Strings::doesNotContain('START A STRING', 'stringly', true, true)); // bool(true)
var_dump(Strings::doesNotContain('START A STRING', 'string', true, true)); // bool(false)

length

Get string length.

use Esi\Utility\Strings;

echo Strings::length('This is a test.'); // 15

camelCase

Returns a camelCase version of the string.

use Esi\Utility\Strings;

echo Strings::camelCase('background-color'); // 'backgroundColor'
echo Strings::camelCase('σamel  Case'); // 'σamelCase'

ascii

Transliterate a UTF-8 value to ASCII.

use Esi\Utility\Strings;

echo Strings::ascii('ă??șțĂ??ȘȚ', 'ro'); // 'aaistAAIST'

slugify

Transforms a string into a URL or filesystem-friendly string.

use Esi\Utility\Strings;

echo Strings::slugify('A simple title'); // 'a-simple-title'
echo Strings::slugify('This post -- it has a dash'); // 'this-post-it-has-a-dash'
echo Strings::slugify('123----1251251'); // '123-1251251'

echo Strings::slugify('A simple title', '_'); // 'a_simple_title'
echo Strings::slugify('This post -- it has a dash', '_'); // 'this_post_it_has_a_dash'
echo Strings::slugify('123----1251251', '_'); // '123_1251251'

echo Strings::slugify('a-simple-title'); // 'a-simple-title'
echo Strings::slugify('Țh?ș ?ș ă ș?mple ț?țle', '-', 'ro'); // 'this-is-a-simple-title'

randomBytes

Generate cryptographically secure pseudo-random bytes.

use Esi\Utility\Strings;

echo Strings::randomBytes(8);

randomString

Generates a secure random string, based on Strings::randomBytes().

use Esi\Utility\Strings;

echo Strings::randomString(8); // 710969eb

validEmail

Validate an email address using PHP's built-in filter.

use Esi\Utility\Strings;

var_dump(Strings::validEmail('john.smith@gmail.com')); // bool(true)
var_dump(Strings::validEmail('john.smith+label@gmail.com')); // bool(true)
var_dump(Strings::validEmail('john.smith@gmail.co.uk')); // bool(true)
var_dump(Strings::validEmail('j@')); // bool(false)

validJson

Determines if a string is valid JSON.

Deprecated as of 2.0.0

use Esi\Utility\Strings;

var_dump(Strings::validJson('{ "test": { "foo": "bar" } }')); // bool(true)
var_dump(Strings::validJson('{ "": "": "" } }')); // bool(false)

obscureEmail

Obscures an email address.

use Esi\Utility\Strings;

echo Strings::obscureEmail('admin@secondversion.com');
/*
'admin@secondversion.com'
*/

guid

Generate a Globally/Universally Unique Identifier (version 4).

use Esi\Utility\Strings;

echo Strings::guid(); // b7c2b60e-dda4-42e9-9ea0-076a28911cea