PHP String Filter is a library to perform character string transformation using a chain. You can use the most popular filters built into PHP and additional ones added by the author and community.
Support the following input data types: string, integer, float, boolean, null and object (must have a __toString method) Support the following output data types: string, int, float, bool and stringOrNull, intOrNull, floatOrNull
Install in your projects:
composer require php-filter/string
And use:
$filter = Filter::of('/_big_ball_of_mud_/')
->replace('/', '')
->replace('_', '')
->upperWords();
$filter->valueString(); // 'Big Ball Of Mud'
Filter list:
Filter | Input | Output |
---|---|---|
alnum() | LLeMs!ZaF_F3dEX 4 |
LLeMsZaFF3dEX4 |
alnumWith('_') | LLeMs!$%ZaF_F3dEX 4 |
LLeMsZaF_F3dEX4 |
append('Smith') | John |
JohnSmith |
camelize() | primary-getallgroups-sys |
primaryGetallgroupsSys |
extractBetween('<div> ', '</div> ') |
<div>test</div> |
test |
htmlSpecialCharsDecode() | <a href="test">Test</a> |
<a href="test">Test</a> |
htmlSpecialChars() | <a href="test">Test</a> |
<a href="test">Test</a> |
letter() | girl_123 |
girl |
letterWith('_') | girl_123! |
girl_ |
limit(4) | this is |
this |
lowerFirst() | Big Ben |
big Ben |
lower() | Lucy Brown |
lucy brown |
numeric() | a123 |
123 |
numericWith('.') | 10.31 zl |
10.31 |
prepend('John ') | Smith |
JohnSmith |
removeMultipleSpaces() | Replacing multiple spaces |
Replacing multiple spaces |
remove(' Up Front') | Big Design Up Front |
Big Design |
repeat(3) | test |
testtesttest |
replaceRegex('/[^a-zA-Z0-9]/', '') | Big-Design-Up-Front |
BigDesignUpFront |
replace('Design Up Front', 'Ball Of Mud') | Big Design Up Front |
Big Ball Of Mud |
reverse() | test |
tset |
shuffle() | test |
tset |
stripHtml('<b> ') |
<u><b>test</b></u> |
dsadsa |
strPadLeft(12, '0'); | 2/10/2020 |
0002/10/2020 |
strPadRight(12, '0'); | 0002/10/2 |
0002/10/2000 |
substr(0, 4); | test 123 |
test |
trimLeft('.') | .test |
test |
trimRight('.') | test. |
test |
trim() | test |
test |
upperFirst() | lucy |
Lucy |
upper() | lucy Brown |
LUCY BROWN |
upperWords() | lucy lue |
Lucy Lue |
wordWrap(3, '</br> ') |
Big Design Up Front |
Big</br>Design</br>Up</br>Front |
Filter example:
For a list of filters and more examples of their application, see unit tests.
$filter = Filter::of('/_big_ball_of_mud_/')
->replace('/', '')
->replace('_', '')
->upperWords();
$filter->valueString(); // 'Big Ball Of Mud'
An example of a reusable filter grouping:
$groupFilters = function ($value) {
return Filter::of($value)->trim()->upperFirst()->append('.');
};
$filter = $groupFilters(' wikipedia is a free online encyclopedia');
$filter->valueString(); // 'Wikipedia is a free online encyclopedia.'
Example value output:
$filter = Filter::of(10.00)->value()->int() // 10
$filter = Filter::of(10.00)->value()->string() // '10.00'
$filter = Filter::of(true)->value()->string() // 'true'
$filter = Filter::of(null)->value()->intOrNull() // null
Example of value information:
$info = Filter::of('wikipedia is a free online encyclopedia, created and edited by by volunteers')->info();
$info->length(); // 76
$info->wordsCount(); // 12
$info->phaseCount('ee'); // 2
- Description of all filters with examples
- Add more filters
- You tell me...
PHP String Filters is released under the MIT License. See the bundled LICENSE file for details.