fab2s / strings
A purely static String Helper to handle more advanced utf8 string manipulations
Installs: 286 566
Dependents: 2
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 1
pkg:composer/fab2s/strings
Requires
- php: ^7.1|^8.0
- ext-mbstring: *
- fab2s/bom: ^1.0
- fab2s/utf8: ^1.0
Requires (Dev)
- phpunit/phpunit: ~7.0|~8.0
This package is auto-updated.
Last update: 2025-09-28 01:06:46 UTC
README
A purely static String Helper to handle more advanced utf8 string manipulations
Installation
Strings can be installed using composer:
composer require "fab2s/strings"
Strings is also included in OpinHelper which packages several bellow "Swiss Army Knife" level Helpers covering some of the most annoying aspects of php programing, such as UTF8 string manipulation, high precision Mathematics or properly locking a file
Should you need to work with php bellow 7.1, you can still use OpinHelper 0.x
Prerequisites
As it requires Utf8, Strings requires mb_string, ext-intl is auto detected and used when available for UTF-8 Normalization
In practice
Strings implement some basic text manipulation function that can be pretty useful IRL
-
filter(string $string):stringDrops Zero Width white chars, normalizes EOL and Normalize UTF8 if ext-intl is available
-
singleWsIze(string $string, bool $normalize = false, bool $includeTabs = true):stringReplace repeated white-spaces to a single one, preserve original white-spaces unless normalized (every white-spaces to ' '), with or without tabs (\t)
-
singleLineIze(string $string):stringMake string fit in one line by replacing EOLs and white-spaces to normalized single white-spaces
-
dropZwWs(string $string):stringRemove Zero Width white-spaces
-
normalizeWs(string $string, bool $includeTabs = true, int $maxConsecutive = null):stringNormalize white-spaces to a single
by default, include tabs by default -
normalizeEol($string, $maxConsecutive = null, $eol = self::EOL):stringNormalize EOLs to a single LF by default
-
normalizeText(string $text):stringReturn
trim'd andfilter'd $text -
normalizeTitle(string $title):stringReturn
singleLineIze'd,normalizeWs'd,normalizeText'd anducfirst'd $title (" the best ever \t\r\n article"->"The best ever article") -
normalizeName(string $name):stringReturn
ucword'd andnormalizeTitle'd $name ("john \n\t doe "->"John Doe") -
escape(string $string, int $flag = ENT_COMPAT, bool $hardEscape = true):stringhtmlspecialchars() wrapper with UTF8 set as encoding
-
softEscape(string $string, int $flag = ENT_COMPAT):stringShortcut for
escape(string $string, $flag, true) -
unEscape(string $string, int $quoteStyle = ENT_COMPAT):stringhtmlspecialchars_decode() wrapper
-
convert(string $string, string $from = null, string $to = self::ENCODING):stringConvert encoding to UTF8 by default. Basic $from encoding detection using
Strings::detectEncoding() -
detectEncoding(string $string):string|nullDetect encoding by checking
Utf8::isUf8(), then trying with BOMs and ultimately fall back to mb_detect_encoding() with limited charsets first, then more internally in mb_convert_encoding() -
secureCompare(string $test, string $reference):boolPerform a Timing Attack safe string comparison (Truly constant operations comparison)
-
contentHash(string $content):stringReturn a
sha256hash of the $content prefixed with $content length. Indented to quickly and reliably detect $content updates.
Requirements
Strings is tested against php 7.2, 7.3, 7.4 and 8.0
Contributing
Contributions are welcome, do not hesitate to open issues and submit pull requests.
License
Strings is open-sourced software licensed under the MIT license