All Projects → spatie → String

spatie / String

Licence: mit
String handling evolved

Labels

Projects that are alternatives of or similar to String

Stryng
Swift strings taken to a whole new syntax level.
Stars: ✭ 255 (-47.2%)
Mutual labels:  string
Mlib
Library of generic and type safe containers in pure C language (C99 or C11) for a wide collection of container (comparable to the C++ STL).
Stars: ✭ 321 (-33.54%)
Mutual labels:  string
Ustring
The Hoa\Ustring library.
Stars: ✭ 403 (-16.56%)
Mutual labels:  string
Obfuscate
Guaranteed compile-time string literal obfuscation header-only library for C++14
Stars: ✭ 260 (-46.17%)
Mutual labels:  string
Coderchef Kitchen
The official repository for our programming kitchen which consists of 50+ delicious programming recipes having all the interesting ingredients ranging from dynamic programming, graph theory, linked lists and much more. All the articles contain beautiful images and some gif/video at times to help clear important concepts.
Stars: ✭ 306 (-36.65%)
Mutual labels:  string
Tiny Utf8
Unicode (UTF-8) capable std::string
Stars: ✭ 322 (-33.33%)
Mutual labels:  string
type-reverse
🦄 Lightweight reverse utility around strings, arrays, numbers and more.
Stars: ✭ 30 (-93.79%)
Mutual labels:  string
Colorful
Terminal string styling done right, in Python 🐍 🎉
Stars: ✭ 456 (-5.59%)
Mutual labels:  string
Voca
The ultimate JavaScript string library
Stars: ✭ 3,387 (+601.24%)
Mutual labels:  string
Competitive Programming Repository
Competitive Programming templates that I used during the past few years.
Stars: ✭ 367 (-24.02%)
Mutual labels:  string
Kind Of
Get the native JavaScript type of a value, fast. Used by superstruct, micromatch and many others!
Stars: ✭ 268 (-44.51%)
Mutual labels:  string
Multilanguages
Android 多语种适配框架,兼容高版本,适配第三方库语种
Stars: ✭ 299 (-38.1%)
Mutual labels:  string
Underscore.string
String manipulation helpers for javascript
Stars: ✭ 3,355 (+594.62%)
Mutual labels:  string
Naza
🍀 Go basic library. || Go语言基础库
Stars: ✭ 253 (-47.62%)
Mutual labels:  string
Portable Utf8
🉑 Portable UTF-8 library - performance optimized (unicode) string functions for php.
Stars: ✭ 405 (-16.15%)
Mutual labels:  string
nim-ustring
utf-8 string for Nim
Stars: ✭ 12 (-97.52%)
Mutual labels:  string
Bistring
Bidirectionally transformed strings
Stars: ✭ 326 (-32.51%)
Mutual labels:  string
Typeset
Deal with AttributedString efficiently
Stars: ✭ 458 (-5.18%)
Mutual labels:  string
Xorstr
heavily vectorized c++17 compile time string encryption.
Stars: ✭ 435 (-9.94%)
Mutual labels:  string
React Element To Jsx String
Turn a ReactElement into the corresponding JSX string
Stars: ✭ 349 (-27.74%)
Mutual labels:  string

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;

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

You can run the tests with:

vendor/bin/phpunit

Contributing

We are very happy to receive pull requests to add functions to this class. Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].