2017 © Pedro Peláez
 

library string

String handling evolved

image

spatie/string

String handling evolved

  • Monday, July 2, 2018
  • by Spatie
  • Repository
  • 12 Watchers
  • 261 Stars
  • 781,729 Installations
  • PHP
  • 15 Dependents
  • 0 Suggesters
  • 16 Forks
  • 1 Open issues
  • 23 Versions
  • 15 % Grown

The README.md

, (*1)

String handling evolved

Latest Version on Packagist Software License Build Status Quality Score Total Downloads, (*2)

This package provides a handy way to work with strings in php., (*3)

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*4)

Support us

, (*5)

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products., (*6)

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall., (*7)

Install

You can install this package via composer:, (*8)

``` bash composer require spatie/string, (*9)


## Usage First you must wrap a native string in a String-object. This can be done with the `string`-function. ```php string('myFirstString');

From then on you can chain methods like there's no tomorrow:, (*10)

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

Of course you can keep concatenate the output with the .-operator we all know and love., (*11)

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

You can also use offsets to manipulate a string., (*12)

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

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

Provided methods

between

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

Example:, (*13)

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

toUpper

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

Example:, (*14)

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

toLower

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

Example:, (*15)

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

tease

/**
 * 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 = '...')

Example:, (*16)

$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...

replaceFirst

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

Example:, (*17)

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

replaceLast

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

Example:, (*18)

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

prefix

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

Example:, (*19)

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

suffix

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

Example:, (*20)

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

concat

This is identical to the suffix-function., (*21)

possessive

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

Example:, (*22)

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

segment

/**
 * 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)

Related methods:, (*23)

/**
 * 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)

Example:, (*24)

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

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

Example:, (*25)

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

contains

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

Example:, (*26)

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

Integration with underscore.php

In addition to the methods described above, you can also use all string methods provided by Maxime Fabre's underscore package., (*27)

For example:, (*28)

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

Of course, you can chain underscore's methods with our own., (*29)

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

Be aware that some underscore methods do not return a string value. Such methods are not chainable., (*30)

string('freek@spatie.be')->isEmail() // returns true;

Testing

You can run the tests with:, (*31)

vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information on what has changed recently., (*32)

Contributing

Please see CONTRIBUTING for details., (*33)

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities., (*34)

Credits

About Spatie

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website., (*35)

Alternatives

This package is primarily built for usage in our own projects. If you need a more full fledged string package take at look at these ones: - Anahkiasen/underscore-php - danielstjules/Stringy, (*36)

License

The MIT License (MIT). Please see License File for more information., (*37)

The Versions

02/07 2018

dev-master

9999999-dev https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

08/11 2017

2.2.2

2.2.2.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

01/12 2016

2.2.1

2.2.1.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

04/10 2016

2.2.0

2.2.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

04/10 2016

dev-analysis-863GKN

dev-analysis-863GKN https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

04/10 2016

dev-analysis-zY2Dwv

dev-analysis-zY2Dwv https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

02/11 2015

2.1.0

2.1.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

22/09 2015

2.0.1

2.0.1.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

13/07 2015

2.0.0

2.0.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

26/06 2015

1.9.1

1.9.1.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

26/06 2015

1.9.0

1.9.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

24/06 2015

1.8.2

1.8.2.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

12/06 2015

1.8.1

1.8.1.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

12/06 2015

1.8.0

1.8.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

09/06 2015

1.7.0

1.7.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

08/06 2015

1.6.0

1.6.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

07/06 2015

1.5.0

1.5.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

07/06 2015

1.4.0

1.4.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

 

The Development Requires

string spatie handling handy

05/06 2015

1.3.0

1.3.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

string spatie handling handy

05/06 2015

1.2.0

1.2.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

string spatie handling handy

05/06 2015

1.1.0

1.1.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

string spatie handling handy

05/06 2015

1.0.0

1.0.0.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

string spatie handling handy

05/06 2015

0.0.1

0.0.1.0 https://github.com/spatie/string

String handling evolved

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

string spatie handling handy