string

String utilities (null_trim, remove_whitespace, truncate_word)

zenstruck/string

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..."

Copyright (c) 2016 Kevin Bond

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.