PHP code example of tcb13 / substringy

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

    

tcb13 / substringy example snippets






use SubStringy\SubStringy as S;

use Stringy\Stringy as S;
echo S::create('Fòô     Bàř', 'UTF-8')->collapseWhitespace()->swapCase();  // 'fÒÔ bÀŘ'

namespace Vendor\YourPackage;

use Stringy\Stringy;
use SubStringy\SubStringyTrait;
use SliceableStringy\SliceableStringyTrait;

class MyStringy extends Stringy
{
    use SubStringyTrait;
    use SliceableStringyTrait;
}

use YourPackage\MyStringy as S;
$sliceableSubstring = S::create('What are your plans today?')->substringAfterFirst('plans ');
echo $sliceableSubstring['4:6'];

S::create('What are your plans today?')->substringAfterFirst('plans ');

S::create('This is a String. How cool can a String be after all?')->substringAfterLast('String ');

S::create('What are your plans today?')->substringBeforeFirst(' plans');

S::create('What are your plans today? Any plans for tomorrow?')->substringBeforeLast(' plans');

S::create('What are your plans today?')->substringBetween('your ', ' today');

S::create('how are you? are you sure you are ok?')->substringCount('are');
 php
$stringy = S::create('Fòô Bàř', 'UTF-8');
foreach ($stringy as $char) {
    echo $char;
}
// 'Fòô Bàř'
 php
$stringy = S::create('Fòô', 'UTF-8');
count($stringy);  // 3
 php
$stringy = S::create('Bàř', 'UTF-8');
echo $stringy[2];     // 'ř'
echo $stringy[-2];    // 'à'
isset($stringy[-4]);  // false

$stringy[3];          // OutOfBoundsException
$stringy[2] = 'a';    // Exception
 php
use function SubStringy\create as s;

// Instead of: S::create('Fòô     Bàř', 'UTF-8')
s('Fòô     Bàř', 'UTF-8')->collapseWhitespace()->swapCase();