An open API service indexing awesome lists of open source software.

https://github.com/flextype-components/strings

Strings Component is a set of methods to help with the manipulation of strings.
https://github.com/flextype-components/strings

Last synced: 5 months ago
JSON representation

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

Awesome Lists containing this project

README

          

Strings Component


Version License Total downloads Quality Score



### Installation

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

```
composer require flextype-components/strings
```

### Usage

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

### Methods

| Method | Description |
|---|---|
| `Strings::stripSpaces()` | Strip all whitespaces from the given string. |
| `Strings::trimSlashes()` | Removes any leading and trailing slashes from a string. |
| `Strings::reduceSlashes()` | Reduces multiple slashes in a string to single slashes. |
| `Strings::stripQuotes()` | Removes single and double quotes from a string. |
| `Strings::quotesToEntities()` | Convert single and double quotes to entities. |
| `Strings::validEncoding()` | Checks if the string is valid in UTF-8 encoding. |
| `Strings::fixEncoding()` | Removes all invalid UTF-8 characters from a string. |
| `Strings::normalizeNewLines()` | Standardize line endings to unix-like. |
| `Strings::normalizeSpaces()` | Normalize white-spaces to a single space. |
| `Strings::random()` | Creates a random string of characters. |
| `Strings::increment()` | Add's `_1` to a string or increment the ending number to allow `_2`, `_3`, etc. |
| `Strings::wordsCount()` | Return information about words used in a string. |
| `Strings::length()` | Return the length of the given string. |
| `Strings::lower()` | Convert the given string to lower-case. |
| `Strings::upper()` | Convert the given string to upper-case. |
| `Strings::limit()` | Limit the number of characters in a string. |
| `Strings::studly()` | Convert a value to studly caps case. |
| `Strings::snake()` | Convert a string to snake case. |
| `Strings::camel()` | Convert a string to camel case. |
| `Strings::kebab()` | Convert a string to kebab case. |
| `Strings::words()` | Limit the number of words in a string. |
| `Strings::contains()` | Determine if a given string contains a given substring. |
| `Strings::containsAll()` | Determine if a given string contains all array values. |
| `Strings::containsAny()` | Determine if a given string contains any of array values. |
| `Strings::substr()` | Returns the portion of string specified by the start and length parameters. |
| `Strings::ucfirst()` | Converts the first character of a UTF-8 string to upper case and leaves the other characters unchanged. |
| `Strings::trim()` | Strip whitespace (or other characters) from the beginning and end of a string. |
| `Strings::trimRight()` | Strip whitespace (or other characters) from the end of a string. |
| `Strings::trimLeft()` | Strip whitespace (or other characters) from the beginning of a string. |
| `Strings::capitalize()` | Converts the first character of every word of string to upper case and the others to lower case. |
| `Strings::reverse()` | Reverses string. |
| `Strings::segments()` | Get array of segments from a string based on a delimiter. |
| `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()` | Get the first segment from a string based on a delimiter. |
| `Strings::lastSegment()` | Get the last segment from a string based on a delimiter. |
| `Strings::between()` | Get the portion of a string between two given values. |
| `Strings::before()` | Get the portion of a string before the first occurrence of a given value. |
| `Strings::beforeLast()` | Get the portion of a string before the last occurrence of a given value. |
| `Strings::after()` | Return the remainder of a string after the first occurrence of a given value. |
| `Strings::afterLast()` | Return the remainder of a string after the last occurrence of a given value. |
| `Strings::padBoth()` | Pad both sides of a string with another. |
| `Strings::padLeft()` | Pad the left side of a string with another. |
| `Strings::padRight()` | Pad the right side of a string with another. |
| `Strings::replaceArray()` | Replace a given value in the string sequentially with an array. |
| `Strings::replaceFirst()` | Replace the first occurrence of a given value in the string. |
| `Strings::replaceLast()` | Replace the last occurrence of a given value in the string. |
| `Strings::start()` | Begin a string with a single instance of a given value. |
| `Strings::startsWith()` | Determine if a given string starts with a given substring. |
| `Strings::endsWith()` | Determine if a given string ends with a given substring. |
| `Strings::finish()` | Cap a string with a single instance of a given value. |
| `Strings::hash()` | Generate a hash string from the input string. |


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

Strip all whitespaces from the given string.

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

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

Removes any leading and trailing slashes from a string.

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

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

Reduces multiple slashes in a string to single slashes.

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

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

Removes single and double quotes from a string.

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

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

Convert single and double quotes to entities.

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

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

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

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

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

Removes all invalid UTF-8 characters from a string.

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

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

Standardize line endings to unix-like.

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

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

Normalize white-spaces to a single space.

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

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

```php
// 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()`

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

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

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

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

Return information about words used in a string

```php
// 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()`

Return the length of the given string.

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

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

Convert the given string to lower-case.

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

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

Convert the given string to upper-case.

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

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

Limit the number of characters in a string.

```php
// 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()`

Convert a value to studly caps case.

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

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

Convert a string to snake case.

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

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

Convert a string to camel case.

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

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

Convert a string to kebab case.

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

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

Limit the number of words in a string.

```php
// 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()`

Determine if a given string contains a given substring.

```php
// 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()`

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

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

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

Determine if a given string contains any of array values.

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

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

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

```php
// 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()`

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Reverses string.

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

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

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

```php
// 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()`

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.

```php
// 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()`

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

```php
// 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()`

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

```php
// 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()`

Get the portion of a string between two given values.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Pad both sides of a string with another.

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

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

Pad the right side of a string with another.

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

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

Pad the left side of a string with another.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Determine if a given string starts with a given substring.

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

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

Determine if a given string ends with a given substring.

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

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

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

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

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

Generate a hash string from the input string.

```php
// 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
[The MIT License (MIT)](https://github.com/flextype-components/strings/blob/master/LICENSE.txt)
Copyright (c) 2020 [Sergey Romanenko](https://github.com/Awilum)