String Utility

Collection of string helpers.

class Jar\Utilities\Utilities\StringUtility

Jar\Utilities\Utilities\StringUtility::crop($value, int $maxCharacters = 150)

Crops a string.

Parameters
  • $value (string) -- The string to crop.

  • $maxCharacters (int) -- Length of cropping.

Returns

The cropped string.

Example:

var_dump(StringUtility::crop('Lorem ipsum dolor sit amet.', 20));
// 'Lorem ipsum dolor...'

// Respects also crops in Tags
var_dump(StringUtility::crop('<h1>Lorem ipsum dolor sit.</h1>', 20));
// '<h1>Lorem ipsum dolor...</h1>'

Jar\Utilities\Utilities\StringUtility::ripTags($string)

Same as "strip_tags" but leaves spaces at the position of removed tags.

Parameters
  • $string (string) -- The string to strip.

Returns

The stripped string.

Example:

var_dump(strip_tags('<span>Hello</span><span>World</span>'));
// 'HelloWorld'

var_dump(StringUtility::ripTags('<span>Hello</span><span>World</span>'));
// 'Hello World'

Jar\Utilities\Utilities\StringUtility::fastSanitize($string, $toLowerCase = true)

Simple sanitizing of strings, no complex handling of umlauts like "äöü".

Parameters
  • $string (string) -- The string to sanitize.

  • $toLowerCase (bool) -- Should the string converted to lower case?

Returns

The sanitized string.

Example:

var_dump(StringUtility::fastSanitize('Über wie viele Brücken musst du gehen?', true));
// 'ber_wie_viele_br_cken_musst_du_gehen'

var_dump(StringUtility::fastSanitize('Über wie viele Brücken musst du gehen?', false));
// 'ber_wie_viele_Br_cken_musst_du_gehen'

Jar\Utilities\Utilities\StringUtility::sanitize($string, $toLowerCase = true)

More complex sanitizing of strings, also handles of umlauts like "äöü".

Parameters
  • $string (string) -- The string to sanitize.

  • $toLowerCase (bool) -- Should the string converted to lower case?

Returns

The sanitized string.

Example:

var_dump(StringUtility::sanitize('Über wie viele Brücken musst du gehen?', true));
// 'ueber_wie_viele_bruecken_musst_du_gehen'

var_dump(StringUtility::sanitize('Über wie viele Brücken musst du gehen?', false));
// 'UEber_wie_viele_Bruecken_musst_du_gehen'