Download the PHP package lucleroy/php-strings without Composer
On this page you can find all versions of the php package lucleroy/php-strings. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download lucleroy/php-strings
More information about lucleroy/php-strings
Files in lucleroy/php-strings
Package php-strings
Short Description PHP library to manipulate strings.
License LGPL-3.0
Informations about the package php-strings
PHP Strings
PHP library to manipulate strings.
Table of contents
- Introduction
- Requirements
- Installation
- Usage
- Basics
- Working with one substring
- Working with multiple substrings
- Case transformers
- Reference
- Strings
- alignLeft
- alignRight
- append
- ascii
- caseTransform
- charAt
- center
- chars
- clear
- contains
- containsAll
- containsAny
- convert
- countOf
- cut
- endsWith
- endsWithAny
- ensureLeft
- ensureRight
- escapeControlChars
- explode
- getEncoding
- is
- isAny
- isAscii
- isEmpty
- isSubstringOf
- isSubstringOfAll
- isSubstringOfAny
- length
- lines
- lower
- lowerFirst
- occurences
- patch
- prepend
- repeat
- repeatToSize
- replace
- replaceAll
- reverse
- select
- separate
- shuffle
- split
- squeeze
- startsWith
- startsWithAny
- surroundWith
- titleize
- toString
- toBinary
- toCaseSensitive
- toCaseInsensitive
- toEncoding
- toUnicode
- transform
- trim
- trimLeft
- trimRight
- truncate
- truncateLeft
- truncateMiddle
- upper
- upperFirst
- Substring builder
- after
- afterFirst
- afterLast
- at
- atEndOfFirst
- atEndOfLast
- atStartOfFirst
- atStartOfLast
- before
- beforeLast
- beforeNext
- betweenSubstrings
- build
- end
- first
- from
- fromFirst
- fromLast
- fromLeft
- fromLength
- grow
- insideSubstrings
- isEmpty
- last
- length
- longestCommonPrefix
- longestCommonSubstring
- longestCommonSuffix
- patch
- remove
- select
- selection
- shiftLeft
- shiftRight
- shrink
- start
- to
- toLast
- toLength
- toNext
- toRight
- toString
- Substring Lists
- afterSubstringAt
- beforeSubstringAt
- convert
- count
- end
- getString
- implode
- info
- patch
- remove
- start
- substringAt
- toArray
- transform
- Strings
Introduction
This library provides an unified way to manage binary strings and Unicode strings.
You can work with case sentive or case insensitive strings.
In addition, it provides powerful methods for working with substrings.
Requirements
- PHP 7.
- mbstring extension.
- intl extension.
Installation (with Composer)
Add the following to the require
section of your composer.json file
and run composer update
.
Usage
Basics
This library provides four main classes implementing StringInterface
:
CaseSensitiveBinaryString
to work with case sensitive binary strings.CaseInsensitiveBinaryString
to work with case insensitive binary strings.CaseSensitiveUnicodeString
to work with case sensitive Unicode strings.CaseInsensitiveUnicodeString
to work with case insensitive Unicode strings.
Important note: These classes are immutable.
Use the create
method to create a string:
By default, Unicode strings use an UTF-8 encoding. If you want to use another encoding you can specify it as a second
arguments to create
. You can use any encoding supporting by the mbstring
extension (not necessary an Unicode encoding):
You can convert any type of string to any other type by using methods
toCaseSensitive
, toCaseInsensitive
, toBinary
, toUnicode
.
Instead of using classes directly, you can use functions s
for binary strings and u
for Unicode strings.
These functions are shortcuts to CaseSensitiveBinaryString:create
and CaseSensitiveUnicodeString:create
respectively.
Working with one substring
At first glance, it seems that this library does not provide methods for searching or extracting substrings. This is of course wrong. But the way to work with a substring is a bit peculiar.
To work with a substring, you must first call the select
method which create a builder (immutable):
Then you use the builder methods to select a substring:
Then you can get information about the position of the substring with selection
, start
, end
, length
:
Note: The end of the selection is the first index after the selection.
You can also modify the substring with patch
or remove it with remove
:
You can extract the substring with build
:
As build
returns an instance of the same type than the original string, you can then transform the substring:
Finally, you can reinject the modified substring into the original string with patch
:
Of course, you can chain operations:
Working with multiple substrings
Some methods (explode
, split
, occurences
, ...) return several substrings. The result is an instance of
a class implementing SubstringListInterface
which extends StringInterface
, so you can use all the methods
available for strings on the result of these methods:
Then use patch
to patch the original string with the modified substrings:
If you don't care of the original string, you can retrieve the substrings as an array (of StringInterface
) with toArray
.
You can also use implode
to join the substrings into a single StringInterface
.
Case transformers
If you want to transform a string to camel case, pascal case, ..., you must use the caseTransform
.
The argument of this method must implement CaseTransformerInterface
(in namespace LucLeroy\Strings\CaseTransformer
).
Common transformers are available via the CaseTransformerFactory
:
caseTransform
keep only letter sequences and digit sequences and provides these sequences as an array of StringInterface
.
CaseTransformerInterface
has a method transform
which transform this array into a StringInterface
.
To implement your own transformer, you can implement CaseTransformerInterface
directly.
Most of the time, you can determine how to render a sequence based on the sequence itself and the two sequences next to it.
For example, in camel case, the first sequence must be in lowercase and others in lowercase except for the first character which must be in uppercase.
You can implement a case transformer depending only on the context by extending AbstractContextCaseTransformer
.
You must implement the transformPart
method which has three arguments: the current part, the previous one, and the next one.
A basic camel case transformer can be implemented as follows:
But there is even simpler: you can create a case transformer with the class SimpleCaseTransformer
.
You can rewrite the previous camel case transformer as follows:
With SimpleCaseTransformer
you can specify a different case for the first sequence and the others, and a different separator
between diffrent types of sequences (letters or digits).
Reference
Strings
alignLeft($size, $fill = ' '): StringInterface
Returns a left-justified string of a given minimum size. The remaining space is filled with the string $fill
;
alignRight($size, $fill = ' '): StringInterface
Returns a right-justified string of a given minimum size. The remaining space is filled with the string $fill
;
append($string): StringInterface
Adds $string
to the end of the current string.
ascii(): UnicodeStringInterface
Available for Unicode strings only.
Converts the string to ASCII.
caseTransform($transformer): StringInterface
Converts the string to camel case, pascal case, kebab case, ...
charAt($index): string
Returns the character at the specified index as an ordinary string.
center($size, $fill = ' '): StringInterface
Returns a centered string of a given minimum size. The remaining space is filled with the string $fill
;
chars(): string[]
Returns the characters composing the string as an array of ordinary strings.
clear(): StringInterface
Returns an empty string.
contains($substring): bool
Determines if $substring
is a substring of the current string.
containsAll($substrings): bool
Determines if all the $substring
are substrings of the current string.
containsAny($substrings): bool
Determines if any of the $substring
i susbstring of the current string.
convert($callable)
Applies a custom tranformation to the string. The result can be anything.
countOf($substring): int
Returns the number of occurrences of $substring
in the current string.
cut($cuts): SubstringListInterface
Returns a SubstringListInterface
containing the current string cut to the specified positions.
endsWith($substring): bool
Determines if the current string ends with $substring
.
endsWithAny($substrings): bool
Determines if the current string ends with any of $substrings
.
ensureLeft($substring): StringInterface
If the current string does not start with $substring
, adds $substring
to the beginning of the current string.
ensureRight($substring): StringInterface
If the current string does not end with $substring
, adds $substring
to the end of the current string.
escapeControlChars(): StringInterface
Escapes control characters.
explode($delimiter, $limit = PHP_INT_MAX): SubstringListInterface
Splits the string by a delimiter.
Note: For a case insensitive string, all versions of the delimiter are replaced by the gicen version:
getEncoding(): string
Available for Unicode strings only.
Returns the encoding of the string.
is($string): bool
Determines is the string is equal to $string
.
isAny($strings): bool
Determines if the string is any of the $strings
.
isAscii(): bool
Determines if the string contains only ASCII characters.
isEmpty(): bool
Determines if the string is empty.
isSubstringOf($string): bool
Determines if the string is a substring of $string
.
isSubstringOfAll($strings): bool
Determines if the string is a substring of each of the strings in $strings
.
isSubstringOfAny($strings): bool
Determines if the string is a substring of any of the strings in $strings
.
length(): int
Returns the length of the string.
lines(): SubstringListInterface
Splits the string to lines. Supported EOL are: "\n", "\r\n", "\r".
lower(): StringInterface
Converts the string to lowercase.
lowerFirst(): StringInterface
Converts the first character of the string to lowercase.
occurences($substrings): SubstringListInterface
Returns all occurences of strings in $substrings
.
patch(): StringInterface
Applies changes made to the substring to the parent string.
If the string is not a substring, return $this
.
prepend($string): StringInterface
Adds $string
to the beginning of the current string.
repeat($multiplier = 2): StringInterface
Returns the string repeated $multiplier
times.
repeatToSize($size): StringInterface
Repeats the string up to the given size.
replace($string): StringInterface
Replaces the current string with $string
.
replaceAll($search, $replace): StringInterface
Replaces all occurrences of the $search
string(s) with the $replace
string(s).
Works in the same way as PHP function str_replace
.
reverse()
Reverses the characters of the string.
select($offset = 0): SubstringBuilder
Starts the selection of a substring, beginning at the given $offset
.
separate($delimiters): SubstringListInterface
Splits the string by multiple delimiters.
shuffle(): StringInterface
Randomly shuffles the string.
split($size = 1): SubstringListInterface
Splits the string in substrings of $size
characters.
squeeze($char = ' '): StringInterface
Replaces consecutive occurences of $char
with one character only.
startsWith($substring): bool
Determines if the string starts with $substring
.
startsWithAny($substrings): bool
Determines if the string starts with any of the strings in $substrings
.
surroundWith($string1, $string2 = null): StringInterface
Adds $string1
to the beginning of the string and $string2
(or $string1
is $string2
is null
) to the end.
titleize(): StringInterface
Converts first word of each word to uppercase, and the others to lowercase.
toString(): string
Converts the string to an ordinary string.
toBinary(): BinarfyStringInterface
Available for Unicode strings only.
Converts an Unicode string to a binary string.
toCaseSensitive(): CaseSensitiveInterface
Available for case insensitive strings only.
Converts a case insensitive string to a case sensitive string.
toCaseInsensitive(): CaseInsensitiveInterface
Available for case sensitive strings only.
Converts a case sensitive string to a case insensitive string.
toEncoding($encoding): UnicodeStringInterface
Available for Unicode strings only.
Modifies the encoding of the string.
toUnicode($encoding = 'UTF-8'): UnicodeStringInterface
Available for binary strings only.
Converts a binary string to an Unicode string with the specified $encoding
.
transform($callable): StringInterface
Applies a custom tranformation to the string. The result must have the same type as the current string. If it is not true, it is converted.
trim($charlist = null): StringInterface
Strips whitespaces or characters in $charlist
if not null
from both the beginning an the end of the string.
trimLeft($charlist = null): StringInterface
Strips whitespaces or characters in $charlist
if not null
from the beginning of the string.
trimRight($charlist = null): StringInterface
Strips whitespaces or characters in $charlist
if not null
from the end of the string.
truncate($size, $string = ''): StringInterface
Truncates on the right to $size
characters. If $string
is not empty,
deleted characters are replaced with it (additional characters are remove
so that the length of the string does not exceed the given size).
truncateLeft($size, $string = ''): StringInterface
Truncates on the left to $size
characters. If $string
is not empty,
deleted characters are replaced with it (additional characters are remove
so that the length of the string does not exceed the given size).
truncateMiddle($size, $string = ''): StringInterface
Truncates on the middle to $size
characters. If $string
is not empty,
deleted characters are replaced with it (additional characters are remove
so that the length of the string does not exceed the given size).
upper(): StringInterface
Converts the string to uppercase.
upperFirst(): StringInterface
Converts the first character of the string to upppercase.
Substring builder
To create a substring builder, use the select
method on the string.
The methods of the substring builder allow you to set a start index and a end index for the substring.
Note that:
- The end index correspond to the index of the first excluded character.
- If you provide an offset to the
select
method, indices are numbered from this offset. For example if the offset is 5, an index of 2 in the substring builder corresponds to an index of 7 in the string.
after($index): SubstringBuilderInterface
Moves the start index just after the index $index
.
afterFirst($substring): SubstringBuilderInterface
Moves the start index just after the last character of the first occurence of $substring
.
afterLast($substring): SubstringBuilderInterface
Moves the start index just after the last character of the last occurence of $substring
.
at($index): SubstringBuilderInterface
Moves the start index and the end index to the index $index
.
The selection is empty.
Useful to insert a string at a specific position.
atEndOfFirst($substring): SubstringBuilderInterface
Moves the start index and the end index just after the last character of the first occurence of $substring
.
The selection is empty.
Useful to insert a string after a specific substring.
atEndOfLast($substring): SubstringBuilderInterface
Moves the start index and the end index just after the last character of the last occurence of $substring
.
The selection is empty.
Useful to insert a string after a specific substring.
atStartOfFirst($substring): SubstringBuilderInterface
Moves the start index and the end index at the first character of the first occurence of $substring
.
The selection is empty.
Useful to insert a string before a specific substring.
atStartOfLast($substring): SubstringBuilderInterface
Moves the start index and the end index at the first character of the last occurence of $substring
.
The selection is empty.
Useful to insert a string before a specific substring.
before($index): SubstringBuilderInterface
Moves the end index to the specified index.
beforeLast($substring): SubstringBuilderInterface
Moves the end index to the first char of the last occurence of $substring
.
beforeNext($substring, $includeStart = false): SubstringBuilderInterface
Moves the end index to the first char of the first occurence of $substring
from the current start index.
If $includeStart
is true
, the search start at the start index. Otherwise, the search starts at the next character.
betweenSubstrings($open, $close, $match = false): SubstringBuilderInterface
If $match
is false, moves the start index and the end index so they select the first susbtring beginning
with $open
and finishing with $close
.
If $match
is true, moves the start index and the end index so they select the first susbtring beginning
with $open
and finishing with $close
matching $open
.
build(): StringInterface
Creates a StringInterface
from the selection.
end(): int
Returns the end index of the selection.
first($substring): SubstringBuilderInterface
Selects the first occurrence of $substring
.
from($index): SubstringBuilderInterface
Moves the start index to $index
.
fromFirst($substring): SubstringBuilderInterface
Move the start index to the first character of the first occurence of $substring
.
fromLast($substring): SubstringBuilderInterface
Moves the start index to the first character of the last occurence of $substring
.
fromLeft(): SubstringBuilderInterface
Moves the start index to the beginning of the string.
fromLength($length): SubstringBuilderInterface
Moves the start index so that the selection length is $length
.
grow($count): SubstringBuilderInterface
Expands the current selection by $count
characters on each side.
insideSubstrings($open, $close, $match = false): SubstringBuilderInterface
If $match
is false, moves the start index and the end index so they select the first susbtring preceded
by $open
and followed by $close
.
If $match
is true, moves the start index and the end index so they select the first susbtring preceded
by $open
and followed by $close
matching $open
.
isEmpty(): bool
Determines if the current selection is empty.
last($substring): SubstringBuilderInterface
Selects the last occurrence of $substring
.
length(): int
Returns the length of the selection.
longestCommonPrefix($string): SubstringBuilderInterface
Selects the longest string common to the current string and $string
, starting from the beginning of each string.
longestCommonSubstring($string): SubstringBuilderInterface
Selects the longest string common to the current string and $string
.
longestCommonSuffix($string): SubstringBuilderInterface
Selects the longest string common to the current string and $string
, finishing at the end of each string.
patch($patch): StringInterface
Returns a StringInterface
with the selection replaced by $patch
.
remove(): StringInterface
Returns a StringInterface
with the selection removed.
select($offset = 0): SubstringBuilderInterface
Start a new selection from the current selection. It is equivalent to do build
followed by select
.
selection(): array | null
If the selection is valid, returns an array containing the start index and the end index in this order.
If the selection is invalid (for example if you try to select a substring which does not exist), returns null
.
shiftLeft($count): SubstringBuilderInterface
Shifts the start index from $count
characters. $count
can be negative.
shiftRight($count): SubstringBuilderInterface
Shifts the end index from $count
characters. $count
can be negative.
shrink($count): SubstringBuilderInterface
Shrinks the current selection by $count
characters on each side.
start(): int
Return the current start index.
to($index): SubstringBuilderInterface
Moves the end index just after the character at index $index
.
toLast($substring): SubstringBuilderInterface
Moves the end index just after the last character of the last occurrence of $substring
.
toLength($length): SubstringBuilderInterface
Moves the end index so that the selection length is $length
.
toNext($substring, $includeStart = false): SubstringBuilderInterface
Moves the end index just after the last character of the first occurrence of $substring
from the start index.
If $includeStart
is true
, the search start at the start index. Otherwise, the search starts at the next character.
toRight($substring): SubstringBuilderInterface
Moves the end index to the end of the string.
toString(): string
Return the selected substring as an ordinary string.
Substring Lists
Classes CaseSensitiveBinarySubstringList
, CaseInsensitiveBinarySubstringList
,
CaseSensitiveUnicodeSubstringList
, CaseInsensitiveUnicodeSubstringList
implements the same methods as the corresponding
StringInterface
.
This section describes additional methods and methods with an altered behavior.
afterSubstringAt($index): StringInterface
Returns the string just after the substring at index $index
(and before the next substring).
beforeSubstringAt($index): StringInterface
Returns the string just before the substring at index $index
(and after the previous substring).
convert($callable): array
Same as convert, but $callable
accepts a second argument containing information
about the current substring. This additional argument is an instance of SubstringInfo
.
count(): int
Returns the number of substrings. You can also use the PHP function count
.
end(): array
Returns indices of the character following each substring.
getString(): StringInterface
Returns the original string.
implode($separator): StringInterface
Join substrings with $separator
.
info($template = null): array
Returns information about each substring. If $template
is null
, the returned array contains instances of
SubstringInfo
. If you provide a template, the returned array items are build according to the template.
patch(): StringInterface
Applies substrings changes to the original string.
remove(): StringInterface
Remove substrings from the original string.
start(): array
Returns indices of the first character of each substring.
substringAt($index): StringInterface
Returns the substring at index $index
.
toArray(): array
Returns an array of substrings.
transform($callable): SubstringListInterface
Same as transform, but $callable
accepts a second argument containing information
about the current substring. This additional argument is an instance of SubstringInfo
.