PHP code example of lucleroy / php-strings
1. Go to this page and download the library: Download lucleroy/php-strings library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
lucleroy / php-strings example snippets
use function LucLeroy\Strings\s;
use function LucLeroy\Strings\u;
echo s('Hello')->upper()->reverse(); // OLLEH
echo u('Доброе утро.')->upper()->reverse(); // .ОРТУ ЕОРБОД
use function LucLeroy\Strings\s;
echo s('Hello World')->contains('hello') ? 'true' : 'false'; // false
echo s('Hello World')->toCaseInsensitive()->contains('hello') ? 'true' : 'false'; // true
use function LucLeroy\Strings\s;
echo s('Hello World')->explode(' ')->reverse()->patch(); // olleH dlroW
use LucLeroy\Strings\CaseSensitiveBinaryString;
$str = CaseSensitiveBinaryString::create('Hello');
use LucLeroy\Strings\CaseSensitiveUnicodeString;
$str = CaseSensitiveUnicodeString::create('Hello…', 'HTML-ENTITIES');
echo $str->append(' World!')->upper(); // HELLO… WORLD!
$str = s('Hello World!');
$builder = $str->select();
$builder = $builder->first('World'); // select the substring 'World'
echo $builder->start(); // 6
echo $builder->end(); // 11
echo $builder->length(); // 5
$selection = $builder->selection(); // $selection = [6, 11]
echo $builder->patch('Everybody'); // Hello Everybody!
echo $builder->remove(); // Hello !
$substring = $builder->build();
echo $substring; // World
$substring = $substring->upper();
echo $substring; // WORLD
echo $substring->patch(); // Hello WORLD!
echo s('Hello World!')
->select()
->first('World')
->build()
->upper()
->patch(); // Hello WORLD!
$str = s('hello world!');
$list = $str->occurences(['h', 'w'])->upper();
echo $list->patch(); // Hello World!
use LucLeroy\Strings\CaseTransformer\CaseTransformerFactory;
use function LucLeroy\Strings\s;
$str = s('Hello World!');
$factory = CaseTransformerFactory::getInstance();
echo $str->caseTransform($factory->camel()); // helloWorld
echo $str->caseTransform($factory->pascal()); // HelloWorld
echo $str->caseTransform($factory->pascal()); // HELLO_WORLD
use LucLeroy\Strings\CaseTransformer\AbstractContextCaseTransformer;
use LucLeroy\Strings\StringInterface;
use function LucLeroy\Strings\s;
class CamelCaseTransformer extends AbstractContextCaseTransformer
{
public function transformPart(
StringInterface $current,
StringInterface $previous = null,
StringInterface $next = null
) {
if ($previous) {
return $current->titleize();
} else {
return $current->lower();
}
}
}
echo s('Hello World!')->caseTransform(new CamelCaseTransformer()); // helloWorld
use LucLeroy\Strings\CaseTransformer\SimpleCaseTransformer;
use function LucLeroy\Strings\s;
$camelCaseTransformer = new SimpleCaseTransformer(
SimpleCaseTransformer::CASE_TITLE,
SimpleCaseTransformer::CASE_LOWER
);
echo s('Hello World!')->caseTransform($camelCaseTransformer);
echo s('Hello')->alignLeft(10); // 'Hello '
echo s('Hello')->alignLeft(10, '.'); // 'Hello.....'
echo s('Hello')->alignLeft(10, '+-'); // 'Hello+-+-+'
echo s('Hello')->alignRight(10); // ' Hello'
echo s('Hello')->alignRight(10, '.'); // '.....Hello'
echo s('Hello')->alignRight(10, '+-'); // '+-+-+Hello'
echo s('Hello')->append(' World!'); // Hello World!
echo u('Доброе утро.')->ascii(); // Dobroe utro.
use LucLeroy\Strings\CaseTransformer\CaseTransformerFactory;
use function LucLeroy\Strings\s;
$factory = CaseTransformerFactory::getInstance();
$camel = $factory->camel();
echo s('Hello World!')->caseTransform($camel); // helloWorld
echo s('Hello')->charAt(1); // 'e'
echo s('Hello')->center(10); // ' Hello '
echo s('Hello')->center(10, '.'); // '..Hello...'
echo s('Hello')->center(10, '+-'); // '+-Hello+-+'
$chars = s('Hello')->chars; // $chars = ['H', 'e', 'l', 'l', 'o']
echo s('Hello')->clear; // ''
echo s('Hello World!')->contains('Hello') ? 'true' : 'false'; // true
echo s('Hello World!')->containsAll(['Hello', 'World]) ? 'true' : 'false'; // true
echo s('Hello World!')->containsAll(['Hello', 'Everybody]) ? 'true' : 'false'; // false
echo s('Hello World!')->containsAny(['Hello', 'World]) ? 'true' : 'false'; // true
echo s('Hello World!')->containsAny(['Hello', 'Everybody]) ? 'true' : 'false'; // true
echo u('Доброе утро.')->convert(function ($s) {
return strlen($s);
}); // 22 (number)
echo s('To be or not to be.')->countOf('be'); // 2
$result = s('Hello World!')->cut([5, 6, 11])->toString(); // $result = ['Hello', ' ', 'World', '!']
echo s('Hello World!')->endsWith('Hello') ? 'true' : 'false'; // false
echo s('Hello World!')->endsWith('World!') ? 'true' : 'false'; // true
echo s('Hello World!')->endsWithAny(['Everybody!', 'World!']) ? 'true' : 'false'; // true
echo s('Hello World!')->ensureLeft('Hello'); // Hello World!
echo s(' World!')->ensureLeft('Hello'); // Hello World!
echo s('Hello World!')->ensureRight('World!'); // Hello World!
echo s('Hello ')->ensureRight('World!'); // Hello World!
echo s("Hello\nWorld!")->escapeControlChars(); // Hello\nWorld!
$result = s('123,456,789')->explode(',')->toString(); // $result = [123, 456, 789]
echo s('123o456O789')->toCaseInsensitive()->explode('o')->patch(); // 123o456o789
echo u('Hello')->getEncoding(); // UTF-8
echo s('Hello World!')->is('Hello World!') ? 'true' : 'false'; // true
echo s('Hello World!')->is('hello world!') ? 'true' : 'false'; // false
echo s('Hello World!')->toCaseInsensitive()->is('hello world!') ? 'true' : 'false'; // true
echo s('Hello')->isAny(['hello', 'world']) ? 'true' : 'false'; // false
echo s('Hello')->toCaseInsensitive()->isAny(['hello', 'world']) ? 'true' : 'false'; // true
echo u('Hello.')->isAscii() ? 'true' : 'false'; // true
echo u('Доброе утро.')->isAscii() ? 'true' : 'false'; // false
echo s('Hello.')->isEmpty() ? 'true' : 'false'; // false
echo s('Hello.')->clear()->isEmpty() ? 'true' : 'false'; // true
echo s('Hello')->isSubstringOf('Hello World!') ? 'true' : 'false'; // true
echo s('Hello')->isSubstringOfAll('Hello World!', 'Hello Everybody!') ? 'true' : 'false'; // true
echo s('Hello')->isSubstringOfAll('Hello World!', 'Good Morning, Vietnam') ? 'true' : 'false'; // false
echo s('Hello')->isSubstringOfAny('Hello World!', 'Good Morning, Vietnam') ? 'true' : 'false'; // true
echo s('Hello')->length(); // 5
$lines = s("Hello\nWorld!")->lines()->toString(); // $line s= ['Hello', 'World!']
echo s("HELLO")->lower(); // hello
echo s("HELLO")->lower(); // hELLO
echo s('Hello World!')->occurences(['o', 'l'])->implode(); // llool
echo s('Hello World!')
->select()->beforeNext(' ')->build()
->upper()->patch(); // HELLO World!
echo s('World!')->prepend('Hello '); // 'Hello World!'
echo s('+-')->repeat(5); // +-+-+-+-+-
echo s('+-')->repeatToSize(15); // +-+-+-+-+-+-+-+
echo s('Hello')->replace('Good Morning'); // Good Morning
echo s('Hello World')->replaceAll(['o', 'l'], '*'); // He*** W*r*d
echo s('Hello World!')->reverse(); // !dlroW olleH
echo s('123 456,789')->separate([' ', ','])->reverse()->patch(); // 321 654,987
echo s('Hello World!')->shuffle(); // rloodl!He lW
echo s('Hello World!')->split(3)->implode('|'); // Hel|lo |Wor|ld!
echo s('Hello World!')->squeeze(); // Hello World!
echo s('Hello World!')->startsWith('Hello') ? 'true' : 'false'; // true
echo s('Hello World!')->startsWith('World!') ? 'true' : 'false'; // false
echo s('Hello World!')->startsWithAny(['Everybody!', 'World!']) ? 'true' : 'false'; // true
echo s('Hello World!')->surroundWith('"', '"'); // "Hello World!"
echo s('hELLO world!')->titleize(); // Hello World!
echo s('Hello World!')->toString(); // Hello World!
$str = u('Доброе утро.');
echo $str->length(); // 12
echo $str->toBinary()->length(); // 22
$str = u('Hello World!')->toCaseInsensitive();
echo $str->startsWith('hello') ? 'true' : 'false'; // true
echo $str->toCaseSensitive()->startsWith('hello') ? 'true' : 'false'; // false
$str = u('Hello World!');
echo $str->startsWith('hello') ? 'true' : 'false'; // false
echo $str->toCaseInsensitive()->startsWith('hello') ? 'true' : 'false'; // true
echo u('«Hello World!»')->toEncoding('HTML'); // «Hello World!»
$str = s('Доброе утро.');
echo $str->length(); // 22
echo $str->toUnicode()->length(); // 12
echo s('Hello World')->transform(function ($s) {
return md5($s);
})->upper(); // B10A8DB164E0754105B7A99BE72E3FE5
echo s('***Hello World!***')->trim('*'); // Hello World!
echo s('***Hello World!***')->trimLeft('*'); // Hello World!***
echo s('***Hello World!***')->trimRight('*'); // ***Hello World!
echo s('Hello World!')->truncate(8, '...'); // Hello...
echo s('Hello World!')->truncateLeft(8, '...'); // ...orld!
echo s('Hello World!')->truncateMiddle(8, '...'); // He...ld!
echo s("hello")->lower(); // HELLO
echo s("hello")->lower(); // Hello
echo s('foo,bar')->select()->after(3); // bar
echo s('foo,bar,foo,baz')->select()->afterFirst('foo'); // ,bar,foo,baz
echo s('foo,bar,foo,baz')->select()->afterLast('foo'); // ,baz
echo s('foo,bar')->select()->at(3)->patch(',baz'); // foo,baz,bar
echo s('foo,bar,foo')->select()->atEndOfFirst('foo')->patch(',baz'); // foo,baz,bar,foo
echo s('foo,bar,foo')->select()->atEndOfLast('foo')->patch(',baz'); // foo,bar,foo,baz
echo s('foo,bar,foo')->select()->atStartOfFirst('foo')->patch('baz,'); // baz,foo,bar,foo
echo s('foo,bar,foo')->select()->atStartOfLast('foo')->patch('baz,'); // foo,bar,baz,foo
echo s('foo,bar')->select()->before(3); // foo
echo s('foo,bar,foo')->select()->beforeLast('foo'); // foo,bar,
echo s('foo,bar,foo,bar')->select()->beforeNext('bar'); // foo,
echo s('foo,bar,foo,bar')->select()->beforeNext('foo'); // foo,bar,
echo s('foo,bar,foo,bar')->select()->beforeNext('foo', true); // <empty string>
echo s('foo,(bar,(foo),bar')->select()->betweenSubstrings('(', ')', true); // (foo)
echo s('foo,(bar,(foo),bar')->select()->betweenSubstrings('(', ')'); // (bar,(foo)
echo s('foo,bar,baz')->select()->first('bar')->build()->upper(); // BAR
echo s('foo,bar,baz')->select()->first('bar')->end(); // 7
echo s('foo,bar,foo,bar')->select()->first('bar')->patch('***'); // foo,***,foo,bar
echo s('foo,bar')->select()->from(3); // ,bar
echo s('foo,bar,baz')->select()->fromFirst(','); // ,bar,baz
"lucleroy/php-strings": "*"
php
echo s('foo,bar,baz')->select()->fromLast(','); // ,baz
php
echo s('foo,bar')->select()->from(3)->fromLeft(); // foo,bar
php
echo s('foo,bar,baz')->select()->beforeLast(',')->fromLength(3); // bar
php
echo s('foo,bar,baz')->select()->afterFirst(',')->beforeNext(',')->grow(1); // ,bar,
php
echo s('foo,(bar,(foo),bar')->select()->insideSubstrings('(', ')', true); // foo
echo s('foo,(bar,(foo),bar')->select()->insideSubstrings('(', ')'); // bar,(foo
php
echo s('foo,bar')->select()->first('baz')->isEmpty() ? 'true' : 'false'; // true
echo s('foo,bar')->select()->from(10)->to(15)->isEmpty() ? 'true' : 'false'; // true
echo s('foo,bar')->select()->from(3)->to(5)->isEmpty() ? 'true' : 'false'; // false
php
echo s('foo,bar,baz')->select()->first('bar')->length(); // 3
php
echo s('foo,bar,baz')->select()->longestCommonPrefix('foo,baz,bar'); // foo,ba
php
echo s('foo,bar,baz')->select()->longestCommonSubstring('bar,baz,foo'); // bar,baz
php
echo s('foo,bar,baz')->select()->longestCommonSuffix('bar,foo,baz'); // ,baz
php
echo s('foo,bar,baz')->select()->first('bar')->patch('***'); // foo,***,baz
php
echo s('foo,bar,baz')->select()->first('bar')->remove(''); // foo,,baz
php
echo s('foo,bar,baz')->select()->afterFirst(',')->select()->afterFirst(','); // baz
php
$selection = s('foo,bar,baz')->select()->first('bar')->selection(); // $selection = [4, 7]
php
echo s('foo,bar,baz')->select()->from(5)->start(); // 5
echo s('foo,bar,baz')->select()->from(5)->shiftLeft(1)->start(); // 6
echo s('foo,bar,baz')->select()->from(5)->shiftLeft(-1)->start(); // 4
php
echo s('foo,bar,baz')->select()->to(5)->end(); // 6
echo s('foo,bar,baz')->select()->to(5)->shiftRight(1)->end(); // 7
echo s('foo,bar,baz')->select()->to(5)->shiftRight(-1)->end(); // 5
php
echo s('foo,bar,baz')->select()->fromFirst(',')->toNext(',')->shrink(1); // bar
php
echo s('foo,bar,baz')->select()->first('bar')->start(); // 4
php
echo s('foo,bar')->select()->to(3); // foo,
php
echo s('foo,bar,baz')->select()->toLast(','); // foo,bar,
php
echo s('foo,bar,baz')->select()->afterFirst(',')->toLength(3); // bar
php
echo s('foo,bar,foo,bar')->select()->toNext('bar'); // foo,bar
echo s('foo,bar,foo,bar')->select()->toNext('foo'); // foo,bar,foo
echo s('foo,bar,foo,bar')->select()->toNext('foo', true); // foo
php
echo s('foo,bar')->select()->to(3)->toRight(); // foo,bar
php
echo s('foo,bar,baz')->select()->from(4)->to(6)->toString(); // bar
php
echo s('foo,bar;baz')->separate([',', ';'])->afterSubstringAt(0); // ,
echo s('foo,bar;baz')->separate([',', ';'])->afterSubstringAt(1); // ;
echo s('foo,bar;baz')->separate([',', ';'])->afterSubstringAt(2); // <empty string>
php
echo s('foo,bar;baz')->separate([',', ';'])->beforeSubstringAt(0); // <empty string>
echo s('foo,bar;baz')->separate([',', ';'])->beforeSubstringAt(1); // ,
echo s('foo,bar;baz')->separate([',', ';'])->beforeSubstringAt(2); // ;
php
$lengths = s('foo,bar;baz')->separate([',', ';'])->convert(function ($s, $info) {
return $info->length();
}); // $lengths = [3, 3, 3]
php
echo s('foo,bar;baz')->separate([',', ';'])->count(); // 3
echo count(s('foo,bar;baz')->separate([',', ';'])); // 3
php
$result = s('foo,bar;baz')->separate([',', ';'])->end(); // $result = [3, 7, 11]
php
echo s('foo,bar;baz')->separate([',', ';'])->getString(); // foo,bar;baz
php
echo s('foo,bar;baz')->separate([',', ';'])->implode('.'); // foo.bar.baz
php
$info = s('foo,bar;baz')
->separate([',', ';'])
->info(['start' => SubstringListInterface::INFO_START, 'end' => SubstringListInterface::INFO_END]);
// $info = [['start' => 0, 'end' => 3], ['start' => 4, 'end' => 7], ['start' => 8, 'end' => 11]]
php
echo s('foo,bar;baz')->separate([',', ';'])->reverse()->patch(); // oof,rab;zab
php
echo s('foo,bar;baz')->separate([',', ';'])->remove(); // ,;
php
$result = s('foo,bar;baz')->separate([',', ';'])->start(); // $result = [0, 4, 8]
php
echo s('foo,bar;baz')->separate([',', ';'])->substringAt(1); // bar
php
echo s('foo,bar;baz')->separate([',', ';'])->transform(function ($s, $info) {
return $info->index() . ': ' . $s;
})->implode(', '); // 0: foo, 1: bar, 2: baz