zenstruck/string

String utilities

v2.1.0 2019-05-29 18:49 UTC

README

Build Status Code Coverage StyleCI Latest Stable Version License

Various string utility functions for PHP. A Twig Extension is available.

Installation

composer require zenstruck/string

Usage

remove_whitespace

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"

null_trim

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"

truncate_word

Truncates text to a length without breaking words (calls remove_whitespace before truncating).

$ret = truncate_word("      foo       bar  baz", 10); // $ret = "foo bar..."