PHP Class Ouzo\Utilities\Strings

ファイルを表示 Open project: letsdrink/ouzo Class Usage Examples

Public Methods

Method Description
abbreviate ( string $string, string $maxWidth ) : string Abbreviate - abbreviates a string using ellipsis.
appendIfMissing ( string $string, string $suffix ) : string Adds suffix to the string, if string does not end with the suffix already.
appendPrefix ( string $string, string $prefix = '' ) : string Adds prefix to the string.
appendSuffix ( string $string, string $suffix = '' ) : string Adds suffix to the string.
camelCaseToUnderscore ( string $string ) : string Changes camel case string to underscored.
contains ( string $string, string $substring ) : boolean Checks if string contains the substring.
endsWith ( string $string, string $suffix ) : boolean Method checks if string ends with $suffix.
equal ( mixed $object1, mixed $object2 ) : boolean Method checks if string representations of two objects are equal.
equalsIgnoreCase ( string $string1, string $string2 ) : boolean Determines whether two strings contain the same data, ignoring the case of the letters in the strings.
escapeNewLines ( string $string ) : string Changes new lines to <br> and converts special characters to HTML entities.
htmlEntities ( string $text ) : string Alias for htmlentities() with UTF-8 and flags ENT_COMPAT and ENT_SUBSTITUTE (ENT_IGNORE for php <= 5.3).
htmlEntityDecode ( string $text ) : string Alias for html_entity_decode() with UTF-8 and defined flag ENT_COMPAT.
isBlank ( string $string ) : boolean Method checks if string is blank.
isNotBlank ( string $string ) : boolean Method checks if string is not blank.
prependIfMissing ( string $string, string $prefix ) : string Adds prefix to the string, if string does not start with the prefix already.
remove ( string $string, string $toRemove ) : mixed Removes all occurrences of a substring from string.
removeAccent ( string $string ) : string Removes accent from letters in string.
removePrefix ( string $string, string $prefix ) : string Returns a new string without the given prefix.
removePrefixes ( string $string, array $prefixes ) : mixed Removes prefixes defined in array from string.
removeSuffix ( string $string, string $suffix ) : string Returns a new string without the given suffix.
replaceNth ( string $subject, string $search, string $replace, integer $nth ) : string
sprintAssoc ( string $string, array $params ) : string Replace all occurrences of placeholder in string with values from associative array.
sprintAssocDefault ( string $string, array $params, string $default = '' ) : string Replace all occurrences of placeholder in string with values from associative array.
startsWith ( string $string, string $prefix ) : boolean Method checks if string starts with $prefix.
substringAfter ( string $string, string $separator ) : string Gets the substring after the first occurrence of a separator. The separator is not returned.
substringBefore ( string $string, string $separator ) : string Gets the substring before the first occurrence of a separator. The separator is not returned.
tableize ( string $class ) : string Converts a word into the format for an Ouzo table name. Converts 'ModelName' to 'model_names'.
trimToNull ( string $string ) : string Removes control characters from both ends of this string returning null if the string is empty ("") after the trim or if it is null.
underscoreToCamelCase ( string $string ) : string Changes underscored string to the camel case.
uppercaseFirst ( string $string ) : string Uppercase first letter (multi-byte safe).

Method Details

abbreviate() public static method

Example: $result = Strings::abbreviate('ouzo is great', 5); Result: ouzo ...
public static abbreviate ( string $string, string $maxWidth ) : string
$string string
$maxWidth string
return string

appendIfMissing() public static method

Example: $string = 'Daenerys Targaryen'; $stringWithPrefix = Strings::appendMissingSuffix($string, ' Targaryen'); Result: Daenerys Targaryen
public static appendIfMissing ( string $string, string $suffix ) : string
$string string
$suffix string
return string

appendPrefix() public static method

Example: $string = 'Targaryen'; $stringWithPrefix = Strings::appendPrefix($string, 'Daenerys '); Result: Daenerys Targaryen
public static appendPrefix ( string $string, string $prefix = '' ) : string
$string string
$prefix string
return string

appendSuffix() public static method

Example: $string = 'Daenerys'; $stringWithSuffix = Strings::appendSuffix($string, ' Targaryen'); Result: Daenerys Targaryen
public static appendSuffix ( string $string, string $suffix = '' ) : string
$string string
$suffix string
return string

camelCaseToUnderscore() public static method

Example: $string = 'LannistersAlwaysPayTheirDebts'; $underscored = Strings::camelCaseToUnderscore($string); Result: lannisters_always_pay_their_debts
public static camelCaseToUnderscore ( string $string ) : string
$string string
return string

contains() public static method

Checks if string contains the substring.
public static contains ( string $string, string $substring ) : boolean
$string string
$substring string
return boolean

endsWith() public static method

Example: $string = 'StringSuffix'; $result = Strings::endsWith($string, 'Suffix'); Result: true
public static endsWith ( string $string, string $suffix ) : boolean
$string string
$suffix string
return boolean

equal() public static method

Example: $result = Strings::equal('0123', 123); Result: false
public static equal ( mixed $object1, mixed $object2 ) : boolean
$object1 mixed
$object2 mixed
return boolean

equalsIgnoreCase() public static method

Example: $equal = Strings::equalsIgnoreCase('ABC123', 'abc123'); Result: true
public static equalsIgnoreCase ( string $string1, string $string2 ) : boolean
$string1 string
$string2 string
return boolean

escapeNewLines() public static method

Example: $string = "My name is Reek \nit rhymes with leek"; $escaped = Strings::escapeNewLines($string); Result: My name is <strong>Reek</strong>
it rhymes with leek
public static escapeNewLines ( string $string ) : string
$string string
return string

htmlEntities() public static method

Alias for htmlentities() with UTF-8 and flags ENT_COMPAT and ENT_SUBSTITUTE (ENT_IGNORE for php <= 5.3).
public static htmlEntities ( string $text ) : string
$text string
return string

htmlEntityDecode() public static method

Alias for html_entity_decode() with UTF-8 and defined flag ENT_COMPAT.
public static htmlEntityDecode ( string $text ) : string
$text string
return string

isBlank() public static method

Example: $result = Strings::isBlank('0'); Result: false
public static isBlank ( string $string ) : boolean
$string string
return boolean

isNotBlank() public static method

Example: $result = Strings::isNotBlank('0'); Result: true
public static isNotBlank ( string $string ) : boolean
$string string
return boolean

prependIfMissing() public static method

Example: $string = 'Daenerys Targaryen'; $stringWithPrefix = Strings::appendMissingPrefix($string, 'Daenerys '); Result: Daenerys Targaryen
public static prependIfMissing ( string $string, string $prefix ) : string
$string string
$prefix string
return string

remove() public static method

Example: $string = 'winter is coming???!!!'; $result = Strings::remove($string, '???'); Result: winter is coming!!!
public static remove ( string $string, string $toRemove ) : mixed
$string string
$toRemove string
return mixed

removeAccent() public static method

Example: $string = "śżźćółŹĘÀÁÂ"; Result: 'szzcolZEAAA'
public static removeAccent ( string $string ) : string
$string string
return string

removePrefix() public static method

Example: $string = 'prefixRest'; $withoutPrefix = Strings::removePrefix($string, 'prefix'); Result: Rest
public static removePrefix ( string $string, string $prefix ) : string
$string string
$prefix string
return string

removePrefixes() public static method

Example: $string = 'prefixRest'; $withoutPrefix = Strings::removePrefixes($string, array('pre', 'fix')); Result: Rest
public static removePrefixes ( string $string, array $prefixes ) : mixed
$string string
$prefixes array
return mixed

removeSuffix() public static method

Example: $string = 'JohnSnow'; $withoutSuffix = Strings::removeSuffix($string, 'Snow'); Result: John
public static removeSuffix ( string $string, string $suffix ) : string
$string string
$suffix string
return string

replaceNth() public static method

public static replaceNth ( string $subject, string $search, string $replace, integer $nth ) : string
$subject string
$search string
$replace string
$nth integer
return string

sprintAssoc() public static method

Example: $sprintfString = "This is %{what}! %{what}? This is %{place}!"; $assocArray = array( 'what' => 'madness', 'place' => 'Sparta' ); Result: 'This is madness! madness? This is Sparta!'
public static sprintAssoc ( string $string, array $params ) : string
$string string
$params array
return string

sprintAssocDefault() public static method

When no value for placeholder is found in array, a default empty value is used if not otherwise specified. Example: $sprintfString = "This is %{what}! %{what}? This is %{place}!"; $assocArray = array( 'what' => 'madness', 'place' => 'Sparta' ); Result: 'This is madness! madness? This is Sparta!'
public static sprintAssocDefault ( string $string, array $params, string $default = '' ) : string
$string string
$params array
$default string
return string

startsWith() public static method

Example: $string = 'prefixRest'; $result = Strings::startsWith($string, 'prefix'); Result: true
public static startsWith ( string $string, string $prefix ) : boolean
$string string
$prefix string
return boolean

substringAfter() public static method

Gets the substring after the first occurrence of a separator. The separator is not returned.
public static substringAfter ( string $string, string $separator ) : string
$string string
$separator string
return string

substringBefore() public static method

Gets the substring before the first occurrence of a separator. The separator is not returned.
public static substringBefore ( string $string, string $separator ) : string
$string string
$separator string
return string

tableize() public static method

Example: $class = "BigFoot"; $table = Strings::tableize($class); Result: BigFeet
public static tableize ( string $class ) : string
$class string
return string

trimToNull() public static method

Example: $result = Strings::trimToNull(' '); Result: null
public static trimToNull ( string $string ) : string
$string string
return string

underscoreToCamelCase() public static method

Example: $string = 'lannisters_always_pay_their_debts'; $camelcase = Strings::underscoreToCamelCase($string); Result: LannistersAlwaysPayTheirDebts
public static underscoreToCamelCase ( string $string ) : string
$string string
return string

uppercaseFirst() public static method

Example: $string = "łukasz"; Result: 'Łukasz'
public static uppercaseFirst ( string $string ) : string
$string string
return string