A purely static String Helper to handle more advanced utf8 string manipulations

1.0.1 2020-09-27 15:04 UTC

This package is auto-updated.

Last update: 2021-01-18 16:53:24 UTC


Build Status Total Downloads Monthly Downloads Latest Stable Version Scrutinizer Code Quality PRs Welcome License

A purely static String Helper to handle more advanced utf8 string manipulations


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


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):string

    Drops Zero Width white chars, normalizes EOL and Normalize UTF8 if ext-intl is available

  • singleWsIze(string $string, bool $normalize = false, bool $includeTabs = true):string

    Replace 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):string

    Make string fit in one line by replacing EOLs and white-spaces to normalized single white-spaces

  • dropZwWs(string $string):string

    Remove Zero Width white-spaces

  • normalizeWs(string $string, bool $includeTabs = true, int $maxConsecutive = null):string

    Normalize white-spaces to a single by default, include tabs by default

  • normalizeEol($string, $maxConsecutive = null, $eol = self::EOL):string

    Normalize EOLs to a single LF by default

  • normalizeText(string $text):string

    Return trim'd and filter'd $text

  • normalizeTitle(string $title):string

    Return singleLineIze'd, normalizeWs'd, normalizeText'd and ucfirst'd $title (" the best ever \t\r\n article" -> "The best ever article")

  • normalizeName(string $name):string

    Return ucword'd and normalizeTitle'd $name ("john \n\t doe " -> "John Doe")

  • escape(string $string, int $flag = ENT_COMPAT, bool $hardEscape = true):string

    htmlspecialchars() wrapper with UTF8 set as encoding

  • softEscape(string $string, int $flag = ENT_COMPAT):string

    Shortcut for escape(string $string, $flag, true)

  • unEscape(string $string, int $quoteStyle = ENT_COMPAT):string

    htmlspecialchars_decode() wrapper

  • convert(string $string, string $from = null, string $to = self::ENCODING):string

    Convert encoding to UTF8 by default. Basic $from encoding detection using Strings::detectEncoding()

  • detectEncoding(string $string):string|null

    Detect 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):bool

    Perform a Timing Attack safe string comparison (Truly constant operations comparison)

  • contentHash(string $content):string

    Return a sha256 hash of the $content prefixed with $content length. Indented to quickly and reliably detect $content updates.


Strings is tested against php 7.1, 7.2, 7.3 and 7.4


Contributions are welcome, do not hesitate to open issues and submit pull requests.


Strings is open-sourced software licensed under the MIT license