PHP code example of spatie / string

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

    

spatie / string example snippets


string('myFirstString');

echo string('StartMiddleEnd')->between('Start', 'End')->toUpper(); // outputs "MIDDLE"
 
echo 'stuck in the ' . string('StartMiddleEnd')->between('Start', 'End')->toLower() . ' with you';

echo string('hello')[1]->toUpper(); //outputs "E"

$string = string('gray');
$string[2] = 'e';
echo $string->toUpper(); //outputs "GREY"

/**
 * Get the string between the given start and end.
 *
 * @param $start
 * @param $end
 * @return \Spatie\String\String
 */
public function between($start, $end)

string('StartMiddleEnd')->between('Start', 'End')->toUpper(); // MIDDLE

/**
 * Convert the string to uppercase.
 *
 * @return \Spatie\String\String
 */
public function toUpper()

string('string')->toUpper(); // STRING

/**
 * Convert the string to lowercase.
 *
 * @return \Spatie\String\String
 */
public function toLower()

string('STRING')->toLower(); // string

/**
 * Shortens a string in a pretty way. It will clean it by trimming
 * it, remove all double spaces and html. If the string is then still
 * longer than the specified $length it will be shortened. The end
 * of the string is always a full word concatinated with the
 * specified moreTextIndicator.
 *
 * @param int $length
 * @param string $moreTextIndicator
 * @return \Spatie\String\String
 */
public function tease($length = 200, $moreTextIndicator = '...')

$longText = 'Now that there is the Tec-9, a crappy spray gun from South Miami. This gun is advertised as the most popular gun in American crime. Do you believe that shit? It actually says that in the little book that comes with it: the most popular gun in American crime.'
string($longText)->tease(10); // Now that...

/**
 * Replace the first occurrence of a string.
 *
 * @param $search
 * @param $replace
 * @return \Spatie\String\String
 */
public function replaceFirst($search, $replace)

$sentence = 'A good thing is not a good thing.';
string($sentence)->replaceFirst('good', 'bad'); // A bad thing is not a good thing.

/**
 * Replace the last occurrence of a string.
 *
 * @param $search
 * @param $replace
 * @return \Spatie\String\String
 */
public function replaceLast($search, $replace)

$sentence = 'A good thing is not a good thing.';
string($sentence)->replaceLast('good', 'bad'); // A good thing is not a bad thing.

/**
 * Prefix a string.
 *
 * @param $string
 * @return \Spatie\String\String
 */
public function prefix($string)

string('world')->prefix('hello '); //hello world

/**
 * Suffix a string.
 *
 * @param $string
 * @return \Spatie\String\String
 */
public function suffix($string)

string('hello')->suffix(' world'); // hello world

/**
 * Get the possessive version of a string.
 *
 * @return \Spatie\String\String
 */
public function possessive()

string('Bob')->possessive(); // Bob's
string('Charles')->possessive(); // Charles'

/**
 * Get a segment from a string based on a delimiter.
 * Returns an empty string when the offset doesn't exist.
 * Use a negative index to start counting from the last element.
 * 
 * @param string $delimiter
 * @param int $index
 * 
 * @return \Spatie\String\String
 */
public function segment($delimiter, $index)

/**
 * Get the first segment from a string based on a delimiter.
 * 
 * @param string $delimiter
 * 
 * @return \Spatie\String\String
 */
public function firstSegment($delimiter)

/**
 * Get the last segment from a string based on a delimiter.
 * 
 * @param string $delimiter
 * 
 * @return \Spatie\String\String
 */
public function lastSegment($delimiter)

string('foo/bar/baz')->segment('/', 0); // foo
string('foo/bar/baz')->segment('/', 1); // bar
string('foo/bar/baz')->firstSegment('/'); // foo
string('foo/bar/baz')->lastSegment('/'); // baz

/**
 * Pop (remove) the last segment of a string based on a delimiter
 * 
 * @param string $delimiter
 * 
 * @return \Spatie\String\String
 */
public function pop($delimiter)

string('foo/bar/baz')->pop('/'); // foo/bar
string('foo/bar/baz')->pop('/')->pop('/'); // foo

/**
 * Check whether a string contains a substring
 *
 * @param array|string $needle
 * @param bool $caseSensitive
 * @param bool $absolute
 *
 * @return bool
 */
public function contains($delimiter)

string('hello world')->contains('world'); // true
string('hello world')->contains('belgium'); // false

string('i am a slug')->slugify()` // returns 'i-am-a-slug'

string('i am a slug')->slugify()->between('i', 'a-slug`) // returns '-am-'

string('[email protected]')->isEmail() // returns true;