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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-strings

PHP Strings

PHP library to manipulate strings.

Table of contents

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

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:

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:

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.


All versions of php-strings with dependencies

PHP Build Version
Package Version
Requires lucleroy/php-regex Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package lucleroy/php-strings contains the following files

Loading the files please wait ....