Juggling with strings in PHP.
This is a forked version of Christian Johansson's phpStringJuggler library.
Major changes:
- PHP 7 support.
- Composer support.
- PHPunit.
composer require 10quality/php-string-juggler
The idea is the make it easier to process strings in a object-oriented way.
An instantiated class is compatible with a string so you can use all functions for strings on a instantiated class.
Like this:
$string = new StringJuggler\Juggler('DONEC');`
echo strtolower($string);
// Echoes 'donec'
Sometimes examples is the best way to learn.
// Use statement
use StringJuggler\Juggler;
$string = new Juggler('Donec id elit non mi porta gravida at eget metus.');
$before = $string->getBefore(' id');
// $before now equals 'Donec'
$after = $string->getAfter('porta ');
// $after now equals 'gravida at eget metus.'
if (($after = $string->getAfter('porta ')) == 'gravida at eget metus.') {
.. do something
} else {
.. do something else
}
If you need to verify if something was found compare with '' rather than boolean false, like this, or use the isEmpty()
and isNotEmpty()
methods.
if (($after = $string->getAfter('porta ')) != '') {
.. do something
} else {
.. do something else
}
if ($string->getAfter('porta ')->isNotEmpty()) {
.. do something
} else {
.. do something else
}
Because the StringJuggler class will always equal true but not always equal an empty string. That behavior is caused by PHP.
$string = new \StringJuggler\String('Nullam quis risus eget urna mollis ornare vel eu leo. Vestibulum id ligula porta felis euismod semper. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Vestibulum id ligula porta felis euismod semper. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam porta sem malesuada magna mollis euismod. Vestibulum id ligula porta felis euismod semper. Nullam id dolor id nibh ultricies vehicula ut id elit. Donec id elit non mi porta gravida at eget metus. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Curabitur blandit tempus porttitor. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Etiam porta sem malesuada magna mollis euismod. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Nullam id dolor id nibh ultricies vehicula ut id elit. Donec id elit non mi porta gravida at eget metus. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Maecenas sed diam eget risus varius blandit sit amet non magna. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.');`
if (($text = $string->getAfter('risus')->getBefore('vel eu')->getTrimmed()) == 'eget urna mollis ornare') {
.. do something
} else {
.. do something else
}`
- after()
- before()
- getAfter()
- getAfterPosition()
- getBefore()
- getBeforePosition()
- getExplode()
- getIReplace()
- getPregMatches()
- getPregReplace()
- getReplace()
- getString()
- getTrimmed()
- ireplace()
- isEmpty()
- isNotEmpty()
- pregMatch()
- pregReplace()
- replace()
- setString()
- trim()
The MIT License (MIT)
Copyright (c) 2015 Christian Johansson