RSI helpers  0.1
RSI helpers
Str Class Reference

String helpers. More...

Static Public Member Functions

static bool ($value)
 Returns literally 'true' or 'false'. More...
 
static random ($length=32, $chars=null, $time=null)
 Generate a random string. More...
 
static codeName ($index)
 Generate a code-name, based on the index. More...
 
static nullify ($str)
 Transform an empty string to a null. More...
 
static normalize ($str)
 Remove accents from a string. More...
 
static urlify ($str, $replace='-')
 Transform a string to a URL-friendly version. More...
 
static emojify ($str)
 Replace ASCII emoji's by their Unicode equivalent. More...
 
static unleet ($str, $upper=false)
 Remove leet-speak (1337 5p33k) from a string. More...
 
static icompare ($str1, $str2)
 Case insesitive string comparison. More...
 
static pad ($str, $length, $pad='0', $type=STR_PAD_LEFT)
 Pad function that defaults to left padding with zeros. More...
 
static format ($str, $format, $pad=null, $type=STR_PAD_LEFT, $replace=' *')
 Format a string. More...
 
static camel ($str, $delimiters=' -_')
 Converts a delimited string to CamelCase. More...
 
static snake ($str, $delimiter='_')
 Converts a CamelCased string to snake_case. More...
 
static limit ($str, $length, $words=false, $delimiter='…')
 Limit a string to a number of characters. More...
 
static parse ($str)
 Parse a string into variables. More...
 
static strip (&$haystack, $needle)
 Return part of a string until a needle. More...
 
static pop (&$haystack, $needle)
 Return part of a string after a needle. More...
 
static part ($haystack, $needle, $index=0)
 Return part of a string until a needle. More...
 
static insert ($str, $insert, $position, $length=0)
 Insert a string at a certain position in another string. More...
 
static stripQuotes ($str, $quotes=["'",'"'])
 Remove optinal quotes from a string. More...
 
static wrap ($str, $width)
 Wrap a string into multiple lines (better wordwrap()). More...
 
static nl2p ($str)
 Replace all newlines with HTML paragraph tags. More...
 
static nl2pbr ($str)
 Replace all multi newlines with HTML paragraph tags, and single newlines with line breaks. More...
 
static transform ($str, $methods, &$count=null)
 Transforms a string according to a certain function. More...
 
static startsWith ($haystack, $needle)
 Check if a string starts with a specific value. More...
 
static startWith ($haystack, $needle)
 Make sure a string starts with a specific value. More...
 
static endsWith ($haystack, $needle)
 Check if a string ends with a specific value. More...
 
static endWith ($haystack, $needle)
 Make sure a string ends with a specific value. More...
 
static numeric ($value)
 Numeric detection. More...
 
static operator ($ref, $operator,$value, $default=false)
 Evaluate an operator between a value and a reference value. More...
 
static implode ($array, $last_delimiter=null, $delimiter=', ')
 Convert an array to a list. More...
 
static ranges ($array, $delimiter=', ', $separator=' - ')
 Show ranges from a numerical array. More...
 
static replaceDate ($str, $time=null, $open='[', $close=']')
 Replace date tags in a string. More...
 
static attributes ($str)
 Extract attributes from a string. More...
 

Data Fields

const TRANSFORM_HTMLCHARS = 'htmlchars'
 
const TRANSFORM_LCFIRST = 'lcfirst'
 
const TRANSFORM_LOWER = 'lower'
 
const TRANSFORM_NL2BR = 'nl2br'
 
const TRANSFORM_NL2P = 'nl2p'
 
const TRANSFORM_NL2PBR = 'nl2pbr'
 
const TRANSFORM_TRIM = 'trim'
 
const TRANSFORM_UCFIRST = 'ucfirst'
 
const TRANSFORM_UCWORDS = 'ucwords'
 
const TRANSFORM_UPPER = 'upper'
 
const TRANSFORM_URLENCODE = 'urlencode'
 
const TRANSFORM_URLIFY = 'urlify'
 
const ATTRIBUTES_PATTERN = '\\s+(?<key>[\\w\\-]+)(?<value>=(\'([^\']*)\'|"([^"]*)"|(\\w+)))?'
 
const PASSWORD_CHARS = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'
 
const MAX_TIME = 20000000000
 

Detailed Description

String helpers.

Definition at line 8 of file Str.php.

Member Function Documentation

◆ attributes()

static attributes (   $str)
static

Extract attributes from a string.

Parameters
string$strString with attributes.
Returns
array Attributes (assoc. array).

Definition at line 467 of file Str.php.

◆ bool()

static bool (   $value)
static

Returns literally 'true' or 'false'.

Parameters
bool$value
Returns
string

Definition at line 33 of file Str.php.

◆ camel()

static camel (   $str,
  $delimiters = ' -_' 
)
static

Converts a delimited string to CamelCase.

Parameters
string$strDelimited string.
string$delimitersWord delimiters.
Returns
string

Definition at line 184 of file Str.php.

◆ codeName()

static codeName (   $index)
static

Generate a code-name, based on the index.

Parameters
int$index
Returns
string

Definition at line 75 of file Str.php.

◆ emojify()

static emojify (   $str)
static

Replace ASCII emoji's by their Unicode equivalent.

Parameters
string$strString with ASCII emoji's (e.g. ';-)').
Returns
string String with Unicode emoji's (e.g. '😉').

Definition at line 113 of file Str.php.

◆ endsWith()

static endsWith (   $haystack,
  $needle 
)
static

Check if a string ends with a specific value.

Parameters
string$haystackString to search in.
string$needleValue to look for at the end of the haystack.
Returns
bool True if the haystack ends with the needle.

Definition at line 367 of file Str.php.

◆ endWith()

static endWith (   $haystack,
  $needle 
)
static

Make sure a string ends with a specific value.

Parameters
string$haystackBase string.
string$needleValue to check for at the end of the haystack.
Returns
string Base string with the needle added if not present.

Definition at line 376 of file Str.php.

◆ format()

static format (   $str,
  $format,
  $pad = null,
  $type = STR_PAD_LEFT,
  $replace = '*' 
)
static

Format a string.

Parameters
string$strBase string (without formatting).
string$formatFormat, $replace characters are replaced with characters from the $str.
string$padOptional padding (to number of $replace characters in $format).
int$typePadding type.
string$replaceCharacters to replace in the $format.
Returns
string Format with

Definition at line 167 of file Str.php.

◆ icompare()

static icompare (   $str1,
  $str2 
)
static

Case insesitive string comparison.

Parameters
string$str1First string.
string$str2Second string.
Returns
Returns < 0 if str1 is less than str2, > 0 if str1 is greater than str2, and 0 if they are equal.

Definition at line 144 of file Str.php.

◆ implode()

static implode (   $array,
  $last_delimiter = null,
  $delimiter = ',
 
)
static

Convert an array to a list.

Parameters
array$arrayArray with list items.
string$last_delimiterDelimiter for the last item (same as delimiter when empty).
string$delimiterDelimiter for the items.
Returns
string

Definition at line 425 of file Str.php.

◆ insert()

static insert (   $str,
  $insert,
  $position,
  $length = 0 
)
static

Insert a string at a certain position in another string.

Parameters
string$strString to insert to.
string$insertString to insert into $str.
int$positionPosition to insert at (negative = relative to end).
int$lengthLength of the part to replace with the insertion.
Returns
string

Definition at line 265 of file Str.php.

◆ limit()

static limit (   $str,
  $length,
  $words = false,
  $delimiter = '…' 
)
static

Limit a string to a number of characters.

Parameters
string$strOriginal string.
int$lengthMaximum length (including delimiter).
bool$wordsBreak at a word boundary.
string$delimiterCharacter to indicate a limited string.
Returns
string

Definition at line 204 of file Str.php.

◆ nl2p()

static nl2p (   $str)
static

Replace all newlines with HTML paragraph tags.

Parameters
string$strString to format.
Returns
string String with paragraphs (null on effective empty string).

Definition at line 302 of file Str.php.

◆ nl2pbr()

static nl2pbr (   $str)
static

Replace all multi newlines with HTML paragraph tags, and single newlines with line breaks.

Parameters
string$strString to format.
Returns
string String with paragraphs and line breaks (null on effective empty string).

Definition at line 310 of file Str.php.

◆ normalize()

static normalize (   $str)
static

Remove accents from a string.

Parameters
string$str
Returns
string

Definition at line 96 of file Str.php.

◆ nullify()

static nullify (   $str)
static

Transform an empty string to a null.

Parameters
string$str
Returns
string

Definition at line 88 of file Str.php.

◆ numeric()

static numeric (   $value)
static

Numeric detection.

Works only on decimal numbers, plus signs and exponential parts are not allowed.

Parameters
mixed$valueValue to check.
Returns
bool True if the value is a numeric.

Definition at line 385 of file Str.php.

◆ operator()

static operator (   $ref,
  $operator,
  $value,
  $default = false 
)
static

Evaluate an operator between a value and a reference value.

Besides the usual '==', '!=', '>', '>=', '<', '<=', en '' operators there are also some specific operators::

  • '-' : Reference starts with the value.
  • '-' : Reference ends with the value.
  • '*' : Reference contains the value.
  • '//' : Reference matches the regular expression in the value.
    Parameters
    string$refReference value. The value to look at.
    string$operatorOperator.
    string$valueValue sought after in the reference.
    bool$defaultDefault result (unknown operator).
    Returns
    bool True if the value matches the reference according to the operator.

Definition at line 401 of file Str.php.

◆ pad()

static pad (   $str,
  $length,
  $pad = '0',
  $type = STR_PAD_LEFT 
)
static

Pad function that defaults to left padding with zeros.

Parameters
string$str
int$length
string$pad
int$type
Returns
string

Definition at line 155 of file Str.php.

◆ parse()

static parse (   $str)
static

Parse a string into variables.

Basicly parse_str() but with a returned array instead of by reference.

Parameters
string$str
Returns
array

Definition at line 218 of file Str.php.

◆ part()

static part (   $haystack,
  $needle,
  $index = 0 
)
static

Return part of a string until a needle.

Parameters
string$haystackString to search in.
string$needleNeedle to look for.
int$indexNumber of parts/needles to skip.
Returns
string Part of string until the needle (false = index not found).

Definition at line 253 of file Str.php.

◆ pop()

static pop ( $haystack,
  $needle 
)
static

Return part of a string after a needle.

Shorten the string including the needle.

Parameters
string$haystackString to search in.
string$needleNeedle to look for (not found = return string until end).
Returns
string Part of string after the needle (false if needle not found).

Definition at line 239 of file Str.php.

◆ random()

static random (   $length = 32,
  $chars = null,
  $time = null 
)
static

Generate a random string.

Parameters
int$lengthLength of the resulting string.
string$charsCharacters to use. Ranges can be indicated as [{from}-{to}]. E.g. '[0-9][a-f]' for the hexadecimal range. Special cases: '*' all digits + letter (upper + lower case); '+': all digits + lower case letters; '@': all easy distinguishable characters (no 0/1/l/o).
int | bool$timeStart the string with the time encoded in the available characters (makes them sequential). When true, the current time will be taken.
Returns
string

Definition at line 46 of file Str.php.

◆ ranges()

static ranges (   $array,
  $delimiter = ',
,
  $separator = ' - ' 
)
static

Show ranges from a numerical array.

For example [1,2,3,5,6,8,9,10] becomes '1 - 3, 5, 6, 8 - 10'.

Parameters
array$arrayArray with numbers.
string$delimiterDelimiter to use between ranges.
string$separatorSeperator to use between boundaries of a range.
Returns
string

Definition at line 437 of file Str.php.

◆ replaceDate()

static replaceDate (   $str,
  $time = null,
  $open = '[',
  $close = ']' 
)
static

Replace date tags in a string.

Parameters
string$strString with date tags.
int$timeTimestamp to use (empty = now).
string$openOpen tag.
string$closeClose tag.
Returns
string String with tags replaced.

Definition at line 455 of file Str.php.

◆ snake()

static snake (   $str,
  $delimiter = '_' 
)
static

Converts a CamelCased string to snake_case.

Parameters
string$strCamelCased string.
string$delimiterDelimiter to put between the words.
Returns
string

Definition at line 193 of file Str.php.

◆ startsWith()

static startsWith (   $haystack,
  $needle 
)
static

Check if a string starts with a specific value.

Parameters
string$haystackString to search in.
string$needleValue to look for at the start of the haystack.
Returns
bool True if the haystack starts with the needle.

Definition at line 349 of file Str.php.

◆ startWith()

static startWith (   $haystack,
  $needle 
)
static

Make sure a string starts with a specific value.

Parameters
string$haystackBase string.
string$needleValue to check for at the start of the haystack.
Returns
string Base string with the needle added if not present.

Definition at line 358 of file Str.php.

◆ strip()

static strip ( $haystack,
  $needle 
)
static

Return part of a string until a needle.

Shorten the string including the needle.

Parameters
string$haystackString to search in.
string$needleNeedle to look for (not found = return string until end).
Returns
string Part of string until the needle.

Definition at line 228 of file Str.php.

◆ stripQuotes()

static stripQuotes (   $str,
  $quotes = ["'",
'"']   
)
static

Remove optinal quotes from a string.

Quotes are only removed if they appear on both ends of the string.

Parameters
string$strString to remove quotes from.
array$quotesPossible quote characters.
Returns
string String without quotes.

Definition at line 275 of file Str.php.

◆ transform()

static transform (   $str,
  $methods,
$count = null 
)
static

Transforms a string according to a certain function.

Parameters
string$strString to format.
string | array$methodsOne or more format function(s).
int$countNumber of format functions applied (> 0 = ok; 0 = unknown function).
Returns
string Tranformed string.

Definition at line 320 of file Str.php.

◆ unleet()

static unleet (   $str,
  $upper = false 
)
static

Remove leet-speak (1337 5p33k) from a string.

Parameters
string$strString with leet-speak characters.
bool$upperReplace with uppercase letters.
Returns
string String with leet-speak symbols replaced by their normal letter. This is near from perfect, since '13' could be 'LE' or 'B', '1' could be 'I' or 'L', etc).

Definition at line 123 of file Str.php.

◆ urlify()

static urlify (   $str,
  $replace = '-' 
)
static

Transform a string to a URL-friendly version.

Parameters
string$strOriginal string.
string$replaceCharacter to replace non 'word' characters by.
Returns
string URL-friendly string.

Definition at line 105 of file Str.php.

◆ wrap()

static wrap (   $str,
  $width 
)
static

Wrap a string into multiple lines (better wordwrap()).

Parameters
string$strString to wrap.
int$widthMaximum width.
Returns
array Wrapped lines.

Definition at line 284 of file Str.php.

Field Documentation

◆ ATTRIBUTES_PATTERN

const ATTRIBUTES_PATTERN = '\\s+(?<key>[\\w\\-]+)(?<value>=(\'([^\']*)\'|"([^"]*)"|(\\w+)))?'

Definition at line 23 of file Str.php.

◆ MAX_TIME

const MAX_TIME = 20000000000

Definition at line 26 of file Str.php.

◆ PASSWORD_CHARS

const PASSWORD_CHARS = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'

Definition at line 25 of file Str.php.

◆ TRANSFORM_HTMLCHARS

const TRANSFORM_HTMLCHARS = 'htmlchars'

Definition at line 10 of file Str.php.

◆ TRANSFORM_LCFIRST

const TRANSFORM_LCFIRST = 'lcfirst'

Definition at line 11 of file Str.php.

◆ TRANSFORM_LOWER

const TRANSFORM_LOWER = 'lower'

Definition at line 12 of file Str.php.

◆ TRANSFORM_NL2BR

const TRANSFORM_NL2BR = 'nl2br'

Definition at line 13 of file Str.php.

◆ TRANSFORM_NL2P

const TRANSFORM_NL2P = 'nl2p'

Definition at line 14 of file Str.php.

◆ TRANSFORM_NL2PBR

const TRANSFORM_NL2PBR = 'nl2pbr'

Definition at line 15 of file Str.php.

◆ TRANSFORM_TRIM

const TRANSFORM_TRIM = 'trim'

Definition at line 16 of file Str.php.

◆ TRANSFORM_UCFIRST

const TRANSFORM_UCFIRST = 'ucfirst'

Definition at line 17 of file Str.php.

◆ TRANSFORM_UCWORDS

const TRANSFORM_UCWORDS = 'ucwords'

Definition at line 18 of file Str.php.

◆ TRANSFORM_UPPER

const TRANSFORM_UPPER = 'upper'

Definition at line 19 of file Str.php.

◆ TRANSFORM_URLENCODE

const TRANSFORM_URLENCODE = 'urlencode'

Definition at line 20 of file Str.php.

◆ TRANSFORM_URLIFY

const TRANSFORM_URLIFY = 'urlify'

Definition at line 21 of file Str.php.


The documentation for this class was generated from the following file: