Git Product home page Git Product logo

string's Introduction

String handling evolved

Latest Version on Packagist Software License Build Status Quality Score Total Downloads

This package provides a handy way to work with strings in php.

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

Support us

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.

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.

Install

You can install this package via composer:

composer require spatie/string

Usage

First you must wrap a native string in a String-object. This can be done with the string-function.

string('myFirstString');

From then on you can chain methods like there's no tomorrow:

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.

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

You can also use offsets to manipulate a string.

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:

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

toUpper

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

Example:

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

toLower

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

Example:

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:

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

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

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

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

suffix

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

Example:

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

concat

This is identical to the suffix-function.

possessive

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

Example:

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:

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

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:

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:

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.

For example:

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

Of course, you can chain underscore's methods with our own.

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.

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

Testing

You can run the tests with:

vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

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.

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:

License

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

string's People

Contributors

adrianmrn avatar carusogabriel avatar freekmurze avatar patinthehat avatar peter279k avatar sebastiandedeyne avatar willemvb avatar yogasukma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

string's Issues

[Feature Request] Callback on the result of the explode

Hi,
Would it be a good idea to return $this from the explode and provide the possibility to use array_filter or array_map and other native feature on the array or simply a callback for the user defined action.

Usecase:

I have a string say

$sizeStr = "Nice shoes with Size= 12 ,13, 14 , 15, "; 

$filterCallback= function($item){
return true; // just example 
};
$parsedSize = \string($sizeStr)
->lastSegment('Size=')
->trim()
->explode(',')
->arrayFilter()
->arrayMap('trim')
->apply(function($data){

});

Would something like this be useful ?

Wrong example or description for "segment" method?

Why in description of method SEGMENT has two parameters, but one of the examples has three parameters? "->segment('/', 0, true)"

Method description:

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

Example:
string('foo/bar/baz')->segment('/', 0, true); // baz <-- 3 parameters

Why do not use:
string('foo/bar/baz')->segment('/', -1); // baz

Anahkiasen/underscore-php Deprecated

I have started to receive warnings from composer that anahkiasen/underscore-php is now abandoned. No alternative for this package is provided, however Laravel's Str:: helpers provide some alternative functions.

It may be worth removing this dependency and directing people towards the Laravel helpers in the Illuminate\Support package?

Laravel 9 Compatibility

It looks like anahkiasen/underscore-php requires doctrine/inflector ^1.0, and Laravel 9 requires ^2.0 via illuminate/support

Is this functionality that should be pulled native into this package? Or is there an alternate package to use?

Related: #22 #23 #24

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.