fyre/string

A string utility library.

v2.0.11 2024-10-30 10:47 UTC

This package is auto-updated.

Last update: 2024-11-30 10:59:15 UTC


README

FyreString is a free, open-source string manipulation library for PHP.

Table Of Contents

Installation

Using Composer

composer require fyre/string

In PHP:

use Fyre\Utility\Str;

Methods

After

Get the contents of a string after the first occurrence of a substring.

  • $string is the input string.
  • $search is a string representing the value to search for.
$after = Str::after($string, $search);

If the search string is not found, this method will return the original string.

After Last

Get the contents of a string after the last occurrence of a substring.

  • $string is the input string.
  • $search is a string representing the value to search for.
$afterLast = Str::afterLast($string, $search);

If the search string is not found, this method will return the original string.

Before

Get the contents of a string before the first occurrence of a substring.

  • $string is the input string.
  • $search is a string representing the value to search for.
$before = Str::before($string, $search);

If the search string is not found, this method will return an empty string.

Before Last

Get the contents of a string before the last occurrence of a substring.

  • $string is the input string.
  • $search is a string representing the value to search for.
$beforeLast = Str::beforeLast($string, $search);

If the search string is not found, this method will return an empty string.

Camel

Convert a string into camelCase.

  • $string is the input string.
$camel = Str::camel($string);

Capitalize

Capitalize the first character of a string.

  • $string is the input string.
$capitalized = Str::capitalize($string);

Chunk

Split a string into smaller chunks.

  • $string is the input string.
  • $size is a number representing the maximum length of a chunk, and will default to 1.
$chunks = Str::chunks($string, $size);

Contains

Determine whether a string contains a substring.

  • $string is the input string.
  • $search is a string representing the value to search for.
$contains = Str::contains($string, $search);

Contains All

Determine whether a string contains all substrings.

  • $string is the input string.
  • $searches is an array containing search strings.
$containsAll = Str::containsAll($string, $searches);

Contains Any

Determine whether a string contains any substring.

  • $string is the input string.
  • $searches is an array containing search strings.
$containsAny = Str::containsAny($string, $searches);

End

Append a substring to a string (if it does not already end with the substring).

  • $string is the input string.
  • $search is a string representing the value to append.
$end = Str::end($string, $search);

Ends With

Determine whether a string ends with a substring.

  • $string is the input string.
  • $search is a string representing the value to search for.
$endsWith = Str::endsWith($string, $search);

Escape

Escape characters in a string for use in HTML.

  • $string is the input string.
  • $flags is a number representing the flags to use when escaping, and will default to Str::ENT_QUOTES | Str::ENT_HTML5.
$escaped = Str::escape($string, $flags);

Index Of

Get the position of the first occurrence of a substring within a string.

  • $string is the input string.
  • $search is a string representing the value to search for.
  • $start is a number representing the starting offset, and will default to 0.
$indexOf = Str::indexOf($string, $search);

If the search string is not found, this method will return -1.

Is String

Determine whether the value is a string.

  • $value is the value to test.
$isString = Str::isString($value);

Kebab

Convert a string into kebab-case.

  • $string is the input string.
$kebab = Str::kebab($string);

Last Index Of

Get the position of the last occurrence of a substring within a string.

  • $string is the input string.
  • $search is a string representing the value to search for.
  • $start is a number representing the starting offset, and will default to 0.
$lastIndexOf = Str::lastIndexOf($string, $search);

If the search string is not found, this method will return -1.

Length

Get the length of a string (in bytes).

  • $string is the input string.
$length = Str::length($string);

Limit

Limit a string to a specified number of bytes.

  • $string is the input string.
  • $limit is a number representing the number of bytes to split the string after.
  • $append is a string representing the value to append if the string is split, and will default to "".
$limited = Str::limit($string, $limit, $append);

Lower

Convert a string into lowercase.

  • $string is the input string.
$lower = Str::lower($string);

Pad

Pad a string to a specified length.

  • $string is the input string.
  • $length is a number representing the desired length.
  • $padding is a string representing the padding to use, and will default to " ".
$padded = Str::pad($string, $length, $padding);

Pad End

Pad the end of a string to a specified length.

  • $string is the input string.
  • $length is a number representing the desired length.
  • $padding is a string representing the padding to use, and will default to " ".
$padded = Str::padEnd($string, $length, $padding);

Pad Start

Pad the start of a string to a specified length.

  • $string is the input string.
  • $length is a number representing the desired length.
  • $padding is a string representing the padding to use, and will default to " ".
$padded = Str::padStart($string, $length, $padding);

Pascal

Convert a string into PascalCase.

  • $string is the input string.
$pascal = Str::pascal($string);

Random

Generate a random string.

  • $length is the length of the string to generate, and will default to 16.
  • $chars is a string representing the characters to use, and will default to "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWYXZ0123456789".
$random = Str::random($length, $chars);

Repeat

Repeat a string a specified number of times.

  • $string is the input string.
  • $count is a number representing the number of times to repeat the string.
$repeated = Str::repeat($string, $count);

Replace

Search and replace a value within a string.

  • $string is the input string.
  • $search is a string representing the value to replace.
  • $replace is a string representing the replacement value.
$replaced = Str::replace($string, $search, $replace);

Replace Array

Search and replace a value within a string.

  • $string is the input string.
  • $search is a string representing the value to replace.
  • $replacements is an array containing replacement strings.
$replaced = Str::replaceArray($string, $search, $replacements);

Replace At

Replace text within a portion of a string.

  • $string is the input string.
  • $replace is a string representing the replacement value.
  • $position is a number representing the position to replace from.
  • $length is a number representing the length to replace.
$replaced = Str::replaceAt($string, $replace, $position, $length);

Replace Each

Search and replace key/value pairs within a string.

  • $string is the input string.
  • $replacements is an array containing replacements.
$replaced = Str::replaceEach($string, $replacements);

Replace First

Search and replace the first occurrence of a value within a string.

  • $string is the input string.
  • $search is a string representing the value to replace.
  • $replace is a string representing the replacement value.
$replaced = Str::replaceFirst($string, $search, $replace);

Replace Last

Search and replace the last occurrence of a value within a string.

  • $string is the input string.
  • $search is a string representing the value to replace.
  • $replace is a string representing the replacement value.
$replaced = Str::replaceLast($string, $search, $replace);

Reverse

Reverse the contents of a string.

  • $string is the input string.
$reversed = Str::reverse($string);

Shuffle

Shuffle the contents of a string.

  • $string is the input string.
$shuffled = Str::shuffle($string);

Slice

Return a specified portion of a string.

  • $string is the input string.
  • $start is a number representing the starting offset.
  • $length is a number representing the maximum length to return, and will default to PHP_INT_MAX.
$sliced = Str::slice($string, $start, $length);

Slug

Format a string for use in a URL.

  • $string is the input string.
  • $delimiter is a string representing the delimiter to use, and will default to "_".
$slug = Str::slug($string, $delimiter);

Snake

Convert a string into snake_case.

  • $string is the input string.
$snake = Str::snake($string);

Split

Split a string by a specified delimiter.

  • $string is the input string.
  • $delimiter is a string representing the delimiter to split by.
  • $limit is a number representing the maximum number of substrings to return, and will default to PHP_INT_MAX.
$split = Str::split($string, $delimiter, $limit);

Start

Prepend a substring to a string (if it does not already begin with the substring).

  • $string is the input string.
  • $search is a string representing the value to prepend.
$start = Str::start($string, $search);

Starts With

Determine whether a string begins with a substring.

  • $string is the input string.
  • $search is a string representing the value to search for.
$startsWith = Str::startsWith($string, $search);

Title

Capitalize the first character of each word in a string.

  • $string is the input string.
$title = Str::title($string);

Transliterate

Transliterate the characters of a string into ASCII.

  • $string is the input string.
$transliterated = Str::transliterate($string);

Trim

Trim whitespace (or other characters) from the start and end of a string.

  • $string is the input string.
  • $mask is a string representing the characters to trim, and will default to " \t\n\r\0\x0B".
$trimmed = Str::trim($string, $mask);

Trim End

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

  • $string is the input string.
  • $mask is a string representing the characters to trim, and will default to " t\n\r\0\x0B".
$trimmed = Str::trimEnd($string, $mask);

Trim Start

Trim whitespace (or other characters) from the start of a string.

  • $string is the input string.
  • $mask is a string representing the characters to trim, and will default to " \t\n\r\0\x0B".
$trimmed = Str::trimStart($string, $mask);

Upper

Convert a string into uppercase.

  • $string is the input string.
$upper = Str::upper($string);