PHPackages                             flextype-components/strings - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. flextype-components/strings

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

flextype-components/strings
===========================

Strings Component is a set of methods to help with the manipulation of strings.

v1.2.0(5y ago)0157MITPHPPHP &gt;=7.3.0

Since Aug 30Pushed 5y ago1 watchersCompare

[ Source](https://github.com/flextype-components/strings)[ Packagist](https://packagist.org/packages/flextype-components/strings)[ RSS](/packages/flextype-components-strings/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (4)Dependencies (1)Versions (6)Used By (0)

Strings Component
=================

[](#strings-component)

[![Version](https://camo.githubusercontent.com/fc306e479646951b6ac3d6154638e01d0166a15b9bba6d6afd6e6adb23c27c99/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f666c6578747970652d636f6d706f6e656e74732f737472696e67732e7376673f6c6162656c3d76657273696f6e26636f6c6f723d677265656e)](https://github.com/flextype-components/strings/releases) [![License](https://camo.githubusercontent.com/0cb650cae5fbc5287da2a5e73501fac11b3fd01c715f9b8045ac852e1246e241/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f636f6c6f723d677265656e)](https://github.com/flextype-components/strings) [![Total downloads](https://camo.githubusercontent.com/d7ac8864882b5a5fe00bb862c9f76a746f25ece07d27b36c60c3ecdeebf8c628/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f646f776e6c6f6164732f666c6578747970652d636f6d706f6e656e74732f737472696e67732f746f74616c2e7376673f636f6c6f723d677265656e)](https://github.com/flextype-components/strings) [![Quality Score](https://camo.githubusercontent.com/cb7962622aaa872e9d03abf721cdac8e5bcd3699cea0dc5ee3c8aad233054704/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f666c6578747970652d636f6d706f6e656e74732f737472696e67732e7376673f6272616e63683d6d617374657226636f6c6f723d677265656e)](https://scrutinizer-ci.com/g/flextype-components/strings?branch=master)

### Installation

[](#installation)

With [Composer](https://getcomposer.org):

```
composer require flextype-components/strings

```

### Usage

[](#usage)

```
use Flextype\Component\Strings;
```

### Methods

[](#methods)

MethodDescription[`Strings::stripSpaces()`](#strings_stripSpaces)Strip all whitespaces from the given string.[`Strings::trimSlashes()`](#strings_trimSlashes)Removes any leading and trailing slashes from a string.[`Strings::reduceSlashes()`](#strings_reduceSlashes)Reduces multiple slashes in a string to single slashes.[`Strings::stripQuotes()`](#strings_stripQuotes)Removes single and double quotes from a string.[`Strings::quotesToEntities()`](#strings_quotesToEntities)Convert single and double quotes to entities.[`Strings::validEncoding()`](#strings_validEncoding)Checks if the string is valid in UTF-8 encoding.[`Strings::fixEncoding()`](#strings_fixEncoding)Removes all invalid UTF-8 characters from a string.[`Strings::normalizeNewLines()`](#strings_normalizeNewLines)Standardize line endings to unix-like.[`Strings::normalizeSpaces()`](#strings_normalizeSpaces)Normalize white-spaces to a single space.[`Strings::random()`](#strings_random)Creates a random string of characters.[`Strings::increment()`](#strings_increment)Add's `_1` to a string or increment the ending number to allow `_2`, `_3`, etc.[`Strings::wordsCount()`](#strings_wordsCount)Return information about words used in a string.[`Strings::length()`](#strings_length)Return the length of the given string.[`Strings::lower()`](#strings_lower)Convert the given string to lower-case.[`Strings::upper()`](#strings_upper)Convert the given string to upper-case.[`Strings::limit()`](#strings_limit)Limit the number of characters in a string.[`Strings::studly()`](#strings_studly)Convert a value to studly caps case.[`Strings::snake()`](#strings_snake)Convert a string to snake case.[`Strings::camel()`](#strings_camel)Convert a string to camel case.[`Strings::kebab()`](#strings_kebab)Convert a string to kebab case.[`Strings::words()`](#strings_words)Limit the number of words in a string.[`Strings::contains()`](#strings_contains)Determine if a given string contains a given substring.[`Strings::containsAll()`](#strings_containsAll)Determine if a given string contains all array values.[`Strings::containsAny()`](#strings_containsAny)Determine if a given string contains any of array values.[`Strings::substr()`](#strings_substr)Returns the portion of string specified by the start and length parameters.[`Strings::ucfirst()`](#strings_ucfirst)Converts the first character of a UTF-8 string to upper case and leaves the other characters unchanged.[`Strings::trim()`](#strings_trim)Strip whitespace (or other characters) from the beginning and end of a string.[`Strings::trimRight()`](#strings_trimRight)Strip whitespace (or other characters) from the end of a string.[`Strings::trimLeft()`](#strings_trimLeft)Strip whitespace (or other characters) from the beginning of a string.[`Strings::capitalize()`](#strings_capitalize)Converts the first character of every word of string to upper case and the others to lower case.[`Strings::reverse()`](#strings_reverse)Reverses string.[`Strings::segments()`](#strings_segments)Get array of segments from a string based on a delimiter.[`Strings::segment()`](#strings_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.[`Strings::firstSegment()`](#strings_firstSegment)Get the first segment from a string based on a delimiter.[`Strings::lastSegment()`](#strings_lastSegment)Get the last segment from a string based on a delimiter.[`Strings::between()`](#strings_between)Get the portion of a string between two given values.[`Strings::before()`](#strings_before)Get the portion of a string before the first occurrence of a given value.[`Strings::beforeLast()`](#strings_beforeLast)Get the portion of a string before the last occurrence of a given value.[`Strings::after()`](#strings_after)Return the remainder of a string after the first occurrence of a given value.[`Strings::afterLast()`](#strings_afterLast)Return the remainder of a string after the last occurrence of a given value.[`Strings::padBoth()`](#strings_padBoth)Pad both sides of a string with another.[`Strings::padLeft()`](#strings_padLeft)Pad the left side of a string with another.[`Strings::padRight()`](#strings_padRight)Pad the right side of a string with another.[`Strings::replaceArray()`](#strings_replaceArray)Replace a given value in the string sequentially with an array.[`Strings::replaceFirst()`](#strings_replaceFirst)Replace the first occurrence of a given value in the string.[`Strings::replaceLast()`](#strings_replaceLast)Replace the last occurrence of a given value in the string.[`Strings::start()`](#strings_start)Begin a string with a single instance of a given value.[`Strings::startsWith()`](#strings_startsWith)Determine if a given string starts with a given substring.[`Strings::endsWith()`](#strings_endsWith)Determine if a given string ends with a given substring.[`Strings::finish()`](#strings_finish)Cap a string with a single instance of a given value.[`Strings::hash()`](#strings_hash)Generate a hash string from the input string.---

####  Method: `Strings::stripSpaces()`

[](#-method-stringsstripspaces)

Strip all whitespaces from the given string.

```
$string = Strings::stripSpaces('SG-1 returns from an off-world mission');
```

####  Method: `Strings::trimSlashes()`

[](#-method-stringstrimslashes)

Removes any leading and trailing slashes from a string.

```
$string = Strings::trimSlashes('some string here/');
```

####  Method: `Strings::reduceSlashes()`

[](#-method-stringsreduceslashes)

Reduces multiple slashes in a string to single slashes.

```
$string = Strings::reduceSlashes('some//text//here');
```

####  Method: `Strings::stripQuotes()`

[](#-method-stringsstripquotes)

Removes single and double quotes from a string.

```
$string = Strings::stripQuotes('some "text" here');
```

####  Method: `Strings::quotesToEntities()`

[](#-method-stringsquotestoentities)

Convert single and double quotes to entities.

```
$string = Strings::quotesToEntities('some "text" here');
```

####  Method: `Strings::validEncoding()`

[](#-method-stringsvalidencoding)

Checks if the string is valid in UTF-8 encoding.

```
$result = Strings::validEncoding('An UTF-8 string here');
```

####  Method: `Strings::fixEncoding()`

[](#-method-stringsfixencoding)

Removes all invalid UTF-8 characters from a string.

```
$string = Strings::fixEncoding('An invalid UTF-8 string here');
```

####  Method: `Strings::normalizeNewLines()`

[](#-method-stringsnormalizenewlines)

Standardize line endings to unix-like.

```
$string = Strings::normalizeNewLines('SG-1 returns from an off-world mission');
```

####  Method: `Strings::normalizeSpaces()`

[](#-method-stringsnormalizespaces)

Normalize white-spaces to a single space.

```
$string = Strings::normalizeSpaces('SG-1  returns  from  an  off-world  mission');
```

####  Method: `Strings::random()`

[](#-method-stringsrandom)

```
// Get random string with predefined settings
$string = Strings::random();

// Get random string with custom length
$string = Strings::random(10);

// Get random string with custom length and custom keyspace
$string = Strings::random(4, '0123456789');
```

####  Method: `Strings::increment()`

[](#-method-stringsincrement)

Add's `_1` to a string or increment the ending number to allow `_2`, `_3`, etc.

```
// Increment string with predefined settings
$string = Strings::increment('page_1');

// Increment string with custom settings
$string = Strings::increment('page-1', 1, '-');
```

####  Method: `Strings::wordsCount()`

[](#-method-stringswordscount)

Return information about words used in a string

```
// Returns the number of words found
$result = Strings::wordsCount('SG-1 returns from an off-world mission to P9Y-3C3 with Daniel Jackson');

// Returns an array containing all the words found inside the string
$result = Strings::wordsCount('SG-1 returns from an off-world mission to P9Y-3C3 with Daniel Jackson', 1)

// Returns an associative array, where the key is the numeric position of the word inside the string and the value is the actual word itself
$result = Strings::wordsCount('SG-1 returns from an off-world mission to P9Y-3C3 with Daniel Jackson', 2)
```

####  Method: `Strings::length()`

[](#-method-stringslength)

Return the length of the given string.

```
$length = Strings::length('SG-1 returns from an off-world mission to P9Y-3C3');
```

####  Method: `Strings::lower()`

[](#-method-stringslower)

Convert the given string to lower-case.

```
$string = Strings::lower('SG-1 returns from an off-world mission to P9Y-3C3');
```

####  Method: `Strings::upper()`

[](#-method-stringsupper)

Convert the given string to upper-case.

```
$string = Strings::upper('SG-1 returns from an off-world mission to P9Y-3C3');
```

####  Method: `Strings::limit()`

[](#-method-stringslimit)

Limit the number of characters in a string.

```
// Get string with predefined limit settings
$string = Strings::limit('SG-1 returns from an off-world mission to P9Y-3C3');

// Get string with limit 10
$string = Strings::limit('SG-1 returns from an off-world mission to P9Y-3C3', 10);

// Get string with limit 10 and append 'read more...'
$string = Strings::limit('SG-1 returns from an off-world mission to P9Y-3C3', 10, 'read more...');
```

####  Method: `Strings::studly()`

[](#-method-stringsstudly)

Convert a value to studly caps case.

```
$string = Strings::studly('foo_bar');
```

####  Method: `Strings::snake()`

[](#-method-stringssnake)

Convert a string to snake case.

```
$string = Strings::snake('fooBar');
```

####  Method: `Strings::camel()`

[](#-method-stringscamel)

Convert a string to camel case.

```
$string = Strings::camel('foo_bar');
```

####  Method: `Strings::kebab()`

[](#-method-stringskebab)

Convert a string to kebab case.

```
$string = Strings::kebab('fooBar');
```

####  Method: `Strings::words()`

[](#-method-stringswords)

Limit the number of words in a string.

```
// Get the number of words in a string with predefined limit settings
$string = Strings::words('SG-1 returns from an off-world mission to P9Y-3C3');

// Get the number of words in a string with limit 3
$string = Strings::words('SG-1 returns from an off-world mission to P9Y-3C3', 3);

// Get the number of words in a string with limit 3 and append 'read more...'
$string = Strings::words('SG-1 returns from an off-world mission to P9Y-3C3', 3, 'read more...');
```

####  Method: `Strings::contains()`

[](#-method-stringscontains)

Determine if a given string contains a given substring.

```
// Determine if a given string contains a given substring.
$result = Strings::contains('SG-1 returns from an off-world mission to P9Y-3C3', 'SG-1');

// Determine if a given string contains a given array of substrings.
$result = Strings::contains('SG-1 returns from an off-world mission to P9Y-3C3', ['SG-1', 'P9Y-3C3']);
```

####  Method: `Strings::containsAll()`

[](#-method-stringscontainsall)

Determine if a given string contains a given array of substrings.

```
$result = Strings::containsAll('SG-1 returns from an off-world mission to P9Y-3C3', ['SG-1', 'P9Y-3C3']);
```

####  Method: `Strings::containsAny()`

[](#-method-stringscontainsany)

Determine if a given string contains any of array values.

```
$result = Strings::containsAny('SG-1 returns from an off-world mission to P9Y-3C3', ['SG-1', 'P9Y-3C3']);
```

####  Method: `Strings::substr()`

[](#-method-stringssubstr)

Returns the portion of string specified by the start and length parameters.

```
// Returns the portion of string specified by the start 0.
$string = Strings::substr('SG-1 returns from an off-world mission to P9Y-3C3', 0);

// Returns the portion of string specified by the start 0 and length 4.
$string = Strings::substr('SG-1 returns from an off-world mission to P9Y-3C3', 0, 4);
```

####  Method: `Strings::ucfirst()`

[](#-method-stringsucfirst)

Converts the first character of a string to upper case and leaves the other characters unchanged.

```
$string = Strings::ucfirst('daniel');
```

####  Method: `Strings::trim()`

[](#-method-stringstrim)

Strip whitespace (or other characters) from the beginning and end of a string.

```
$string = Strings::trim(' daniel ');
```

####  Method: `Strings::trimRight()`

[](#-method-stringstrimright)

Strip whitespace (or other characters) from the end of a string.

```
$string = Strings::trimRight('daniel ');
```

####  Method: `Strings::trimLeft()`

[](#-method-stringstrimleft)

Strip whitespace (or other characters) from the beginning of a string.

```
$string = Strings::trimLeft(' daniel');
```

####  Method: `Strings::capitalize()`

[](#-method-stringscapitalize)

Converts the first character of every word of string to upper case and the others to lower case.

```
$string = Strings::capitalize('that country was at the same stage of development as the United States in the 1940s');
```

####  Method: `Strings::reverse()`

[](#-method-stringsreverse)

Reverses string.

```
$string = Strings::reverse('SG-1 returns from an off-world mission');
```

####  Method: `Strings::segments()`

[](#-method-stringssegments)

Get array of segments from a string based on a delimiter.

```
// Get array of segments from a string based on a predefined delimiter.
$segments = Strings::segments('SG-1 returns from an off-world mission');

// Get array of segments from a string based on a delimiter '-'.
$segments = Strings::segments('SG-1 returns from an off-world mission', '-');
```

####  Method: `Strings::segment()`

[](#-method-stringssegment)

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.

```
// Get a segment 1 from a string based on a predefined delimiter.
$string = Strings::segment('SG-1 returns from an off-world mission', 1);

// Get a segment 1 from a string based on a delimiter '-'.
$string = Strings::segment('SG-1 returns from an off-world mission', 1, '-');

// Get a segment 1 from a string starting from the last based on a delimiter '-'.
$string = Strings::segment('SG-1 returns from an off-world mission', -1, '-');
```

####  Method: `Strings::firstSegment()`

[](#-method-stringsfirstsegment)

Get the first segment from a string based on a delimiter.

```
// Get a first segment from a string based on a predefined delimiter.
$string = Strings::firstSegment('SG-1 returns from an off-world mission');

// Get a first segment from a string based on a delimiter '-'.
$string = Strings::firstSegment('SG-1 returns from an off-world mission', '-');
```

####  Method: `Strings::lastSegment()`

[](#-method-stringslastsegment)

Get the last segment from a string based on a delimiter.

```
// Get a last segment from a string based on a predefined delimiter.
$string = Strings::lastSegment('SG-1 returns from an off-world mission');

// Get a last segment from a string based on a delimiter '-'.
$string = Strings::lastSegment('SG-1 returns from an off-world mission', '-');
```

####  Method: `Strings::between()`

[](#-method-stringsbetween)

Get the portion of a string between two given values.

```
$string = Strings::between('SG-1 returns from an off-world mission', 'SG-1', 'from');
```

####  Method: `Strings::before()`

[](#-method-stringsbefore)

Get the portion of a string before the first occurrence of a given value.

```
$string = Strings::before('SG-1 returns from an off-world mission', 'mission');
```

####  Method: `Strings::beforeLast()`

[](#-method-stringsbeforelast)

Get the portion of a string before the last occurrence of a given value.

```
$string = Strings::beforeLast('SG-1 returns from an off-world mission', 'mission');
```

####  Method: `Strings::after()`

[](#-method-stringsafter)

Return the remainder of a string after the first occurrence of a given value.

```
$string = Strings::after('SG-1 returns from an off-world mission', 'SG-1');
```

####  Method: `Strings::afterLast()`

[](#-method-stringsafterlast)

Return the remainder of a string after the last occurrence of a given value.

```
$string = Strings::afterLast('SG-1 returns from an off-world mission', 'SG-1');
```

####  Method: `Strings::padBoth()`

[](#-method-stringspadboth)

Pad both sides of a string with another.

```
$string = Strings::padBoth('SG-1 returns from an off-world mission', 50, '-');
```

####  Method: `Strings::padRight()`

[](#-method-stringspadright)

Pad the right side of a string with another.

```
$string = Strings::padRight('SG-1 returns from an off-world mission', 50, '-');
```

####  Method: `Strings::padLeft()`

[](#-method-stringspadleft)

Pad the left side of a string with another.

```
$string = Strings::padLeft('SG-1 returns from an off-world mission', 50, '-');
```

####  Method: `Strings::replaceArray()`

[](#-method-stringsreplacearray)

Replace a given value in the string sequentially with an array.

```
$string = Strings::replaceArray('SG-1 returns from an off-world mission', 'SG-1', ['SG-2']);
```

####  Method: `Strings::replaceFirst()`

[](#-method-stringsreplacefirst)

Replace the first occurrence of a given value in the string.

```
$string = Strings::replaceFirst('SG-1 returns from an off-world mission', 'SG-1', 'SG-2');
```

####  Method: `Strings::replaceLast()`

[](#-method-stringsreplacelast)

Replace the last occurrence of a given value in the string.

```
$string = Strings::replaceLast('SG-1 returns from an off-world mission', 'off-world', 'P9Y-3C3');
```

####  Method: `Strings::start()`

[](#-method-stringsstart)

Begin a string with a single instance of a given value.

```
$string = Strings::start('movies/sg-1/season-5/episode-21/', '/');
```

####  Method: `Strings::startsWith()`

[](#-method-stringsstartswith)

Determine if a given string starts with a given substring.

```
$result = Strings::startsWith('/movies/sg-1/season-5/episode-21/', '/');
```

####  Method: `Strings::endsWith()`

[](#-method-stringsendswith)

Determine if a given string ends with a given substring.

```
$result = Strings::endsWith('/movies/sg-1/season-5/episode-21/', '/');
```

####  Method: `Strings::finish()`

[](#-method-stringsfinish)

Cap a string with a single instance of a given value.

```
$result = Strings::finish('/movies/sg-1/season-5/episode-21', '/');
```

####  Method: `Strings::hash()`

[](#-method-stringshash)

Generate a hash string from the input string.

```
// Get string hash with predefined settings
$result = Strings::hash('SG-1 returns from an off-world mission');

// Get string hash with hashed with sha256 algorithm
$result = Strings::hash('SG-1 returns from an off-world mission', 'sha256');

// Get string hash with hashed with sha256 algorithm and with raw output
$result = Strings::hash('SG-1 returns from an off-world mission', 'sha256', true);
```

### License

[](#license)

[The MIT License (MIT)](https://github.com/flextype-components/strings/blob/master/LICENSE.txt)Copyright (c) 2020 [Sergey Romanenko](https://github.com/Awilum)

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

4

Last Release

2076d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/477114?v=4)[Sergey Romanenko](/maintainers/Awilum)[@Awilum](https://github.com/Awilum)

---

Top Contributors

[![Awilum](https://avatars.githubusercontent.com/u/477114?v=4)](https://github.com/Awilum "Awilum (57 commits)")

---

Tags

phpdotstringsflextype-componentsphp-strings

### Embed Badge

![Health badge](/badges/flextype-components-strings/health.svg)

```
[![Health](https://phpackages.com/badges/flextype-components-strings/health.svg)](https://phpackages.com/packages/flextype-components-strings)
```

###  Alternatives

[doctrine/inflector

PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowercase and singular/plural forms of words.

11.4k855.8M711](/packages/doctrine-inflector)[glowy/strings

Strings Component provide a fluent, object-oriented interface for working with multibyte string, allowing you to chain multiple string operations together using a more readable syntax compared to traditional PHP strings functions.

183.4k2](/packages/glowy-strings)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
