Skip to content

Commit

Permalink
add segments() method
Browse files Browse the repository at this point in the history
  • Loading branch information
Awilum committed Sep 8, 2020
1 parent 51934c9 commit 4c3d039
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ use Flextype\Component\Strings;
| <a href="#strings_trimLeft">`Strings::trimLeft()`</a> | Strip whitespace (or other characters) from the beginning of a string. |
| <a href="#strings_capitalize">`Strings::capitalize()`</a> | Converts the first character of every word of string to upper case and the others to lower case. |
| <a href="#strings_reverse">`Strings::reverse()`</a> | Reverses string. |
| <a href="#strings_segments">`Strings::segments()`</a> | Get array of segments from a string based on a delimiter. |
| <a href="#strings_segment">`Strings::segment()`</a> | 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. |
| <a href="#strings_firstSegment">`Strings::firstSegment()`</a> | Get the first segment from a string based on a delimiter. |
| <a href="#strings_lastSegment">`Strings::lastSegment()`</a> | Get the last segment from a string based on a delimiter. |
Expand Down Expand Up @@ -316,6 +317,18 @@ Reverses string.
$string = Strings::reverse('SG-1 returns from an off-world mission');
```

#### <a name="strings_segments"></a> 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', '-');
```

#### <a name="strings_segment"></a> Method: `Strings::segment()`

Get a segment from a string based on a delimiter.
Expand Down
13 changes: 12 additions & 1 deletion src/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,25 @@ public static function reverse(string $string): string
return $result;
}

/**
* Get array of segments from a string based on a delimiter.
*
* @param string $string String
* @param string $delimiter Delimeter
*/
public static function segments(string $string, string $delimiter = ' '): array
{
return explode($delimiter, $string);
}

/**
* 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 $string String
* @param string $delimiter Delimeter
* @param int $index Index
* @param string $delimiter Delimeter
*/
public static function segment(string $string, int $index, string $delimiter = ' '): string
{
Expand Down

0 comments on commit 4c3d039

Please sign in to comment.