PHP code example of laraplus / string

1. Go to this page and download the library: Download laraplus/string 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/ */

    

laraplus / string example snippets


str(' some text ')->trim()->substring(0, 4);

// instead of

substr(trim(' some text '), 0, 4);

$lastIndex = str($lastSlug)->explode('-')->last();

// instead of

$parts = $explode('-');
$lastIndex = array_pop($parts);

str($content)->lines()->each(function ($row) {
    $data = str($row)->explode(':');
    // do something with $data here
});

// instead of

$lines = preg_split('/\r\n|\n|\r/', $content);
foreach($lines as $line) {
    $data = explode(':', $line);
    // do something with $data here
}

$string = new Laraplus\String\StringBuffer('Hello world!');

// or

$string = str('Hello world!');

// result: "HELLO"
str('hello world')->toUpper()->wordAt(0);

// a string "Hello world" is produced in all examples below
str('hello world')->ucfirst()->get();
(string)str('hello world')->ucfirst();
str('hello')->ucfirst() . ' world!';

str('hello world')->words()->each(function($word){
 // Be careful, $word is just a plain string here.
 // To convert it to StringBuffer again just call: str($word)
});

// result: "Hello world"
str('hello world')->ucfirst();

// result: "hello world"
str('Hello world')->lcfirst();

// returns true
str('Hello world')->startsWith('Hello');

// returns false
str('Hello world')->startsWith('world');

// returns true
str('Hello world')->startsWith(['H', 'W']);

// returns true
str('Hello world')->endsWith('world');

// returns false
str('Hello world')->endsWith('Hello');

// returns true
str('Hello world')->endsWith(['o', 'd']);

// returns true
str('Hello world')->contains('world');

// returns false
str('Hello world')->contains('universe');

// returns true
str('Hello world')->contains(['w', 'u']);

// returns true
str('Hello world')->equals('Hello world');

// returns false
str('Hello world')->equals('Hello universe');

// returns true
str('Hello/World')->matches('*/*');

// returns true
str('Hello world')->equals('Hello*');

// result: ['Hello', ' World']
str('Hello, World')->explode(',');

// result: ['one', 'two', 'three', 'four']
str('one:two,three:four')->explode([':', ',']);

// returns 0
str('one, two')->indexOf('o');

// returns 7
str('one, two')->indexOf('o', 1);

// returns 7
str('one, two')->lastIndexOf('o');

// returns 0
str('one, two')->lastIndexOf('o', 1);

// result: 'one; two; three'
str('one, two, three')->replace(',', ';');

// result: 'one; two; three' and $count in incremented by 2
str('one, two, three')->replace(',', ';', $count);

// result: 'one; two; three'
str('one, two. three')->replace([',', '.'], ';');

// result: 'one; two, three'
str('one, two. three')->replace([',', '.'], [';', ',']);

// result: 'world'
str('Hello world')->substring(6);

// result: 'll'
str('Hello world')->substring(2, 2);

// result: 'CcZzSs'
str('Č莞Šš')->toAscii();

// result: 'helloWorld'
str('hello_world')->toCamel();

// result: 'hello_world'
str('HelloWorld')->toSnake();

// result: 'HelloWorld'
str('hello_world')->toStudly();

// result: 'Hello World'
str('hello world')->toTitle();

// result: 'hello-world'
str('Hello world')->toSlug();

// result: 'HELLO WORLD'
str('Hello world')->toUpper();

// result: 'hello world'
str('Hello World')->toLower();

// result: 'person'
str('people')->toSingular();

// result: 'people'
str('person')->toPlural();

// returns 11
str('Hello world')->length();

// result: ['one', 'two', 'three']
str('one, two, three')->words();

// result: ['one', 'two', 'three']
str('(one) : (two) : (three)')->words('(:)');

// result: ['one', 'two', 'three']
str("one\ntwo\r\nthree")->lines();

// result: 'hello world'
str('world')->prepend(' ')->prepend('hello');

// result: 'hello world'
str('hello')->append(' ')->append('world');

// result: 'hello world'
str('  hello world  ')->trim();

// result: 'hello world'
str('--hello world--')->trim('-');

// result: 'hello world  '
str('  hello world  ')->ltrim();

// result: 'hello world--'
str('--hello world--')->ltrim('-');

// result: '  hello world'
str('  hello world  ')->rtrim();

// result: '--hello world'
str('--hello world--')->rtrim('-');

// result: 'hello...'
str('hello world')->limit(5);

// result: 'hello etc.'
str('hello world')->limit(5, ' etc.');

// result: 'hello the world...'
str('Hello the world of PHP!')->limitWords(3);

// result: 'hello the world etc.'
str('Hello the world of PHP!')->limitWords(3, ' etc.');

// result: 'world'
str('Hello the world of PHP!')->wordAt(2);

//result: ['one', ['two', ['three'], 'four']]
str('one{two{three}four}')->tree();

$string = str('hello world');
$string[0]; // returns 'h'
isset($string[10]) // returns true
unset($string[0]); // $string becomes 'ello world'
$string[0] = 'He'; // $string becomes 'Hello world'