PHP code example of originphp / text

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

    

originphp / text example snippets


$ascii = Text::toAscii('Ragnarr Loðbrók'); // Ragnarr Lodbrok

$slug = Text::slug('Who is Ragnarr Loðbrók?'); // who-is-ragnarr-lodbrok

$result = Text::contains('foo','What is foo bar'); // true

$result = Text::left('foo','What is foo bar'); // 'What is '
$result = Text::right('foo','What is foo bar'); //' bar'

$bool = Text::startsWith('What','What is foo bar'); // true
$bool = Text::endsWith('bar','What is foo bar'); // true

$result = Text::replace('foo','***','What is foo bar'); // 'What is *** bar'
$result = Text::replace('foo','***','What is FOO bar',['insensitive'=>true]); // 'What is *** bar'

$string = Text::insert('Record {id} has been updated',[
    'id'=>1234568
]); // Record 1234568 has been updated

$letter = file_get_contents('/directory/some-file');
$string = Text::insert($letter,[
    'salutation' => 'Mr.',
    'first_name' => 'Tony',
    'last_name' => 'Robbins',
    'address_1' => '100 Santa Monica Road',
]);

$string = Text::insert('Record :id has been updated',[
    'id'=>1234568,'before'=>':','after'=>''
    ]); // Record 1234568 has been updated

$string = '2019-07-10 13:30:00 192.168.1.22 "GET /users/login HTTP/1.0" 200 1024';
$result = Text::tokenize($string,['separator'=>' ']);

/*
// Will give you this
[
    '2019-07-10',
    '13:30:00',
    '192.168.1.22',
    'GET /users/login HTTP/1.0',
    '200',
    '1024'
];
*/

$string = '2019-07-10 13:30:00 192.168.1.22 "GET /users/login HTTP/1.0" 200 1024';
$result = Text::tokenize($string,[
    'separator'=>' ',
    'keys'=>['date','time','ip','request','code','bytes']
]);

/*
// Will give you this
[
    'date'=>'2019-07-10',
    'time'=>'13:30:00',
    'ip' => '192.168.1.22',
    'request' =>'GET /users/login HTTP/1.0',
    'code'=>'200',
    'bytes'=>'1024'
];
*/

$truncated = Text::truncate($string,['length'=>50,'end'=>'... truncated']);

$wrapped = Text::wordWrap($string); // default is 80
$wrapped = Text::wordWrap($string,['width'=>50]);

$lowerCase = Text::toLower($string);
$uppserCase = Text::toUpper($string);
$int = Text::length($string);