Various string utility functions for PHP. A Twig Extension is available.
composer require zenstruck/string
Replaces
with a single space and converts multiple sequential spaces into a
single space.
$ret = remove_whitespace(" foo \n\n\n \r bar"); // $ret = "foo bar"
Similar to core "trim" but returns null instead of an empty string. When an array is passed, all elements get processed recursively.
$ret = null_trim(" foo bar "); // $ret = "foo bar"
$ret = null_trim(" "); // $ret = null
$ret = null_trim(array(" foo bar ", " ")); // $ret = array("foo bar", null)
$ret = null_trim("foo / ", "/ "); // $ret = "foo"
Truncates text to a length without breaking words (calls remove_whitespace
before truncating).
$ret = truncate_word(" foo bar baz", 10); // $ret = "foo bar..."