Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

str

> 编程语言
Open source

A fast, solid and strong typed string manipulation library with multibyte support

187 stars0 likes1 views
WebsiteGitHub

About

A fast, solid and strong typed string manipulation library with multibyte support

About

str/str

php
$str = new Str('Hello, 世界');
$str->last(2); // 世界
$str->chars(); // ['世', '界']

$str
    ->ensureLeft('Hello, ') // Hello, 世界
    ->ensureRight('!!!') // Hello, 世界!!!
    ->trimRight('!') // Hello, 世界
    ->prepend('Str say - '); // Str say - Hello, 世界

$send = function (string $s) {};
$send((string)$str); // same
$send($str->getString()); // same

Install

Requirements:

  • php7.1
bash
composer require str/str

Features

  • strongly typed
  • no exceptions thrown
  • fast
  • new functions

A fast string manipulation library with multi-byte support. Inspired by the "Stringy" library, with focus on speed.

Lib uses php7 features and does not throw any exceptions (because all input parameters are strongly typed). The code is completely covered by unit tests.


Functions Index:

A

  • afterFirst
  • afterLast
  • append
  • appendUniqueIdentifier
  • at

B

  • beforeFirst
  • beforeLast
  • between

C

  • camelize
  • chars
  • chop
  • collapseWhitespace
  • contains
  • containsAll
  • containsAny
  • countSubstr

D

  • dasherize
  • delimit

E

  • endsWith
  • endsWithAny
  • ensureLeft
  • ensureRight

F

  • first

G

  • getString

H

  • hasLowerCase
  • hasPrefix
  • hasSuffix
  • hasUpperCase
  • htmlDecode
  • htmlEncode
  • humanize

I

  • indexOf
  • indexOfLast
  • insert
  • isAlpha
  • isAlphanumeric
  • isBase64
  • isBlank
  • isEmail
  • isHexadecimal
  • isIpV4
  • isIpV6
  • isJson
  • isLowerCase
  • isSerialized
  • isUUIDv4
  • isUpperCase

J

  • join

L

  • last
  • length
  • lines
  • longestCommonPrefix
  • longestCommonSubstring
  • longestCommonSuffix
  • lowerCaseFirst

M

  • make
  • matchesPattern
  • move

O

  • overwrite

P

  • padBoth
  • padLeft
  • padRight
  • pop
  • popReversed
  • prepend

Q

  • quote

R

  • random
  • regexReplace
  • removeLeft
  • removeRight
  • repeat
  • replace
  • replaceWithLimit
  • reverse

S

  • safeTruncate
  • shift
  • shiftReversed
  • shuffle
  • slice
  • slugify
  • snakeize
  • split
  • startsWith
  • startsWithAny
  • stripWhitespace
  • substr
  • surround
  • swapCase

T

  • tidy
  • titleize
  • toAscii
  • toBoolean
  • toLowerCase
  • toSpaces
  • toTabs
  • toTitleCase
  • toUpperCase
  • trim
  • trimLeft
  • trimRight
  • truncate

U

  • underscored
  • unquote
  • upperCamelize
  • upperCaseFirst

W

  • words

Functions List:

afterFirst

Inserts given $substr $times into the original string after the first occurrence of $needle.

php
$str = new Str('foo bar baz');
echo (string)$str->afterFirst('a', 'duh', 2);
// foo baduhduhr baz

Parameters:

  • string $needle
  • string $substr
  • int $times

Return:

  • \Str

afterLast

Inserts given $substr $times into the original string after the last occurrence of $needle.

php
$str = new Str('foo bar baz');
echo (string)$str->afterLast('a', 'duh', 2);
// foo bar baduhduhz

Parameters:

  • string $needle
  • string $substr
  • int $times

Return:

  • \Str

append

Append $sub to the string.

php
$str = new Str('/Acme');
echo (string)$str->append('/');
// /Acme/

Parameters:

  • string $sub

Return:

  • \Str

appendUniqueIdentifier

Appends a random string consisting of $possibleChars, if specified, of given $size or random length between $size and $sizeMax to the original string.

php
$str = new Str('foo');
echo $str->appendUniqueIdentifier(3, -1, 'foba_rz');
// foozro

Parameters:

  • int $size
  • int $sizeMax
  • string $possibleChars

Return:

  • \Str

at

Returns the character at $pos, with indexes starting at 0.

php
$str = new Str('/Acme/');
echo (string)$str->at(2);
// c

Parameters:

  • int $pos

Return:

  • \Str

beforeFirst

Inserts given $substr $times into the original string before the first occurrence of $needle.

php
$str = new Str('foo bar baz');
echo (string)$str->beforeFirst('a', 'duh');
// foo bduhar baz

Parameters:

  • string $needle
  • string $substr
  • int $times

Return:

  • \Str

beforeLast

Inserts given $substr $times into the original string before the last occurrence of $needle.

php
$str = new Str('foo bar baz');
echo (string)$str->beforeLast('a', 'duh');
// foo bar bduhaz

Parameters:

  • string $needle
  • string $substr
  • int $times

Return:

  • \Str

between

Returns the substring between $start and $end, if found, or an empty string. An optional $offset may be supplied from which to begin the search for the start string.

php
$str = new Str('/Acme/');
echo (string)$str->between('/', '/');
// Acme

Parameters:

  • string $start
  • string $end
  • int $offset

Return:

  • \Str

camelize

Returns a camelCase version of the string. Trims surrounding spaces, capitalizes letters following digits, spaces, dashes and underscores, and removes spaces, dashes, as well as underscores.

php
$str = new Str('ac me');
echo (string)$str->camelize();
// acMe

Parameters: nothing

Return:

  • \Str

chars

Returns an array consisting of the characters in the string.

php
$str = new Str('/Acme/');
echo (string)$str->chars();
// ['/', 'A', 'c', 'm', 'e', '/']

Parameters: nothing

Return:

  • array

chop

Cuts the original string in pieces of $step size.

php
$str = new Str('foo bar baz');
echo $str->chop(2);
// ['fo', 'o ', 'ba', 'r ', 'ba', 'z']

Parameters:

  • int $step

Return:

  • array

collapseWhitespace

Trims the string and replaces consecutive whitespace characters with a single space. This includes tabs and newline characters, as well as multi-byte whitespace such as the thin space and ideographic space.

php
$str = new Str('foo bar baz');
echo (string)$str->collapseWhitespace();
// foo bar baz

Parameters: nothing

Return:

  • \Str

contains

Check if the string contains $needle substring.

php
$str = new Str('/Acme/');
echo $str->contains('/');
// true
$str = new Str('/Acme/');
echo $str->contains('a', false);
// true

Parameters:

  • string $needle
  • bool $caseSensitive

Return:

  • bool

containsAll

Returns true if the string contains all $needles, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.

php
$str = new Str('/Accmme/');
echo $str->containsAll(['m', 'c', '/']);
// true

Parameters:

  • array $needles
  • bool $caseSensitive

Return:

  • bool

containsAny

Returns true if the string contains any $needles, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.

php
$str = new Str('/Accmme/');
echo $str->containsAny(['foo', 'c', 'bar']);
// true

Parameters:

  • array $needles
  • bool $caseSensitive

Return:

  • bool

countSubstr

Returns the number of occurrences of $needle in the given string. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.

php
$str = new Str('/Accmme/');
echo $str->countSubstr('m');
// 2

Parameters:

  • string $needle
  • bool $caseSensitive

Return:

  • int

dasherize

Returns a lowercase and trimmed string separated by dashes. Dashes are inserted before uppercase characters (with the exception of the first character of the string), and in place of spaces as well as underscores.

php
$str = new Str('Ac me');
echo (string)$str->dasherize();
// ac-me

Parameters: nothing

Return:

  • \Str

delimit

Returns a lowercase and trimmed string separated by the given $delimiter. Delimiters are inserted before uppercase characters (with the exception of the first character of the string), and in place of spaces, dashes, and underscores. Alpha delimiters are not converted to lowercase.

php
$str = new Str('Ac me');
echo (string)$str->delimit('#');
// ac#me

Parameters:

  • $delimiter

Return:

  • \Str

endsWith

Returns true if the string ends with $substring, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.

php
$str = new Str('/Accmme/');
echo $str->endsWith('e/');
// true

Parameters:

  • string $substring
  • bool $caseSensitive

Return:

  • bool

endsWithAny

Returns true if the string ends with any of $substrings, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.

php
$str = new Str('/Accmme/');
echo $str->endsWithAny(['foo', 'e/', 'bar']);
// true

Parameters:

  • array $substrings
  • bool $caseSensitive

Return:

  • bool

ensureLeft

Check whether $prefix exists in the string, and prepend $prefix to the string if it doesn't.

php
$str = new Str('Acme/');
echo (string)$str->ensureLeft('/');
// /Acme/
$str = new Str('/Acme/');
echo (string)$str->ensureLeft('/');
// /Acme/

Parameters:

  • string $check

Return:

  • \Str

ensureRight

Check whether $suffix exists in the string, and append $suffix to the string if it doesn't.

php
$str = new Str('/Acme');
echo (string)$str->ensureRight('/'); // /Acme/
$str = new Str('/Acme/');
echo (string)$str->ensureRight('/'); // /Acme/

Parameters:

  • string $check

Return:

  • \Str

first

Returns the first $length characters of the string.

php
$str = new Str('/Acme/');
echo (string)$str->first(2);
// /A

Parameters:

  • int $length

Return:

  • \Str

getString

Parameters: nothing

Return:

  • string

hasLowerCase

Returns true if the string contains a lower case char, false otherwise.

php
$str = new Str('Acme');
echo $str->hasLowerCase();
// true

Parameters: nothing

Return:

  • bool

hasPrefix

Check if the string has $prefix at the start.

php
$str = new Str('/Acme/');
echo $str->hasPrefix('/');
// true

Parameters:

  • string $prefix

Return:

  • bool

hasSuffix

Check if the string has $suffix at the end.

php
$str = new Str('/Acme/');
echo $str->hasSuffix('/');
// true

Parameters:

  • string $suffix

Return:

  • bool

hasUpperCase

Returns true if the string contains an upper case char, false otherwise.

php
$str = new Str('Acme');
echo $str->hasUpperCase();
// true

Parameters: nothing

Return:

  • bool

htmlDecode

Convert all HTML entities to their applicable characters. An alias of html_entity_decode. For a list of flags, refer to PHP documentation.

php
$str = new Str('&lt;Acme&gt;');
echo (string)$str->htmlDecode();
// 

Parameters:

  • int $flags

Return:

  • \Str

htmlEncode

Convert all applicable characters to HTML entities. An alias of htmlentities. Refer to PHP documentation for a list of flags.

php
$str = new Str('');
echo (string)$str->htmlEncode();
// &lt;Acme&gt;

Parameters:

  • int $flags

Return:

  • \Str

humanize

Capitalizes the first word of the string, replaces underscores with spaces.

php
$str = new Str('foo_id');
echo (string)$str->humanize();
// Foo

Parameters: nothing

Return:

  • \Str

indexOf

Returns the index of the first occurrence of $needle in the string, and -1 if not found. Accepts an optional $offset from which to begin the search.

php
$str = new Str('/Accmme/');
echo $str->indexOf('m');
// 4

Parameters:

  • string $needle
  • int $offset

Return:

  • int

indexOfLast

Returns the index of the last occurrence of $needle in the string, and false if not found. Accepts an optional $offset from which to begin the search. Offsets may be negative to count from the last character in the string.

php
$str = new Str('/Accmme/');
echo $str->indexOfLast('

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPlibrarymultibytephpphp-library

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言