Documentation

Text
in package

Table of Contents

Constants

FIND_STARTS_WITH  : mixed = 0x1000
Perform finds only against the needle length.
MASK_TYPE_EMAIL  : mixed = 'EMAIL'
MASK_TYPE_FIRSTLASTNAME  : mixed = 'FIRSTLASTNAME'
MASK_TYPE_FIRSTNAME  : mixed = 'FIRSTNAME'
Mask preset types
MASK_TYPE_LASTNAME  : mixed = 'LASTNAME'
MASK_TYPE_PHONE  : mixed = 'PHONE'
MASK_TYPE_SPACES  : mixed = 'SPACES'
NORMALIZE_ALL  : mixed = 0x1 | 0x2 | 0x4
Normalize all the things.
NORMALIZE_ALPHA  : mixed = 0x2
Normalize similar looking characters.
NORMALIZE_CASE  : mixed = 0x1
Set everything to lowercase.
NORMALIZE_MULTIBYTE  : mixed = 0x4
Convert multi-byte to random single-byte characters.

Properties

$_map  : array<string|int, mixed>|null
$ALPHA_RULES  : array<string|int, mixed>
$DISTANCE_FACTOR  : int
$ENCODING  : string
$MIN_SIMILAR_PERCENT  : int

Methods

compare()  : int
Compare the similarity of two strings using similarity and Levenshtein checks
find()  : array<string|int, string>
Find something similar from a list of options.
mask()  : string
Returns given string masked by given character, multi-byte safe
maskPreset()  : string
Applies preset mask to given string @see `Text::mask`
normalize()  : string
Normalize a string with a set of rules.
normalizeAlpha()  : string
Normalize similar looking characters.
normalizeMultibyte()  : string
Replaces multi-byte characters with random single-byte UTF-8 characters.
similar()  : bool
similarity()  : float
Get the similarity between two strings.
startsWith()  : array<string|int, mixed>

Constants

FIND_STARTS_WITH

Perform finds only against the needle length.

public mixed FIND_STARTS_WITH = 0x1000

MASK_TYPE_EMAIL

public mixed MASK_TYPE_EMAIL = 'EMAIL'

MASK_TYPE_FIRSTLASTNAME

public mixed MASK_TYPE_FIRSTLASTNAME = 'FIRSTLASTNAME'

MASK_TYPE_FIRSTNAME

Mask preset types

public mixed MASK_TYPE_FIRSTNAME = 'FIRSTNAME'

MASK_TYPE_LASTNAME

public mixed MASK_TYPE_LASTNAME = 'LASTNAME'

MASK_TYPE_PHONE

public mixed MASK_TYPE_PHONE = 'PHONE'

MASK_TYPE_SPACES

public mixed MASK_TYPE_SPACES = 'SPACES'

NORMALIZE_ALL

Normalize all the things.

public mixed NORMALIZE_ALL = 0x1 | 0x2 | 0x4

NORMALIZE_ALPHA

Normalize similar looking characters.

public mixed NORMALIZE_ALPHA = 0x2

(good for OCR texts).

NORMALIZE_CASE

Set everything to lowercase.

public mixed NORMALIZE_CASE = 0x1

NORMALIZE_MULTIBYTE

Convert multi-byte to random single-byte characters.

public mixed NORMALIZE_MULTIBYTE = 0x4

Properties

$_map

public static array<string|int, mixed>|null $_map = null

Not for public use.

$ALPHA_RULES

public static array<string|int, mixed> $ALPHA_RULES = ["a4 e3 o0D ilL1!| `’", "AA EE OOO IIIIII ''"]

$DISTANCE_FACTOR

public static int $DISTANCE_FACTOR = 10

$ENCODING

public static string $ENCODING = 'UTF-8'

$MIN_SIMILAR_PERCENT

public static int $MIN_SIMILAR_PERCENT = 85

Methods

compare()

Compare the similarity of two strings using similarity and Levenshtein checks

public static compare(string $str1, string $str2[, int $flags = self::NORMALIZE_ALL ]) : int
Parameters
$str1 : string

The first string for comparison

$str2 : string

The string to compare the first with

$flags : int = self::NORMALIZE_ALL

one of the normalisation flags

Return values
int —

lower is better (or -1 if too long)

find()

Find something similar from a list of options.

public static find(string $needle, array<string|int, string> $haystack[, int $max = 5 ][, int $flags = self::NORMALIZE_ALL ]) : array<string|int, string>

Great for a 'did you mean?' type of thing.

Parameters
$needle : string

a fuzzy string

$haystack : array<string|int, string>

set of possible options

$max : int = 5

limit results

$flags : int = self::NORMALIZE_ALL

one of the normalisation flags

Return values
array<string|int, string> —

subset of 'options' in order of closeness

mask()

Returns given string masked by given character, multi-byte safe

public static mask(string $word[, string $mask_char = '*' ][, int $start = 0 ][, int $end = 0 ]) : string
Parameters
$word : string

The string to mask, Eg Apple

$mask_char : string = '*'

Optional masking character. Default of *

$start : int = 0

Optional number of characters to keep from the start of string. Eg 1 = A****

$end : int = 0

Optional number of characters to keep from the end of string. Eg 2 = ***le

Return values
string —

Eg *****|A****|****e|A***e

maskPreset()

Applies preset mask to given string @see `Text::mask`

public static maskPreset(string $word, string $preset[, string $mask_char = '*' ]) : string
Parameters
$word : string
$preset : string

Text::MASK_TYPE_*

$mask_char : string = '*'

Optional masking character. Default of *

Tags
throws
Exception

Unknown preset

Return values
string

normalize()

Normalize a string with a set of rules.

public static normalize(string $str[, int $flags = self::NORMALIZE_ALL ]) : string

Flags:

  • NORMALIZE_CASE
  • NORMALIZE_ALPHA
  • NORMALIZE_MULTIBYTE

Note the output of this function isn't useful for much else but the similarity/compare functions.

Parameters
$str : string
$flags : int = self::NORMALIZE_ALL
Return values
string

normalizeAlpha()

Normalize similar looking characters.

public static normalizeAlpha(string $str) : string
Parameters
$str : string
Return values
string

normalizeMultibyte()

Replaces multi-byte characters with random single-byte UTF-8 characters.

public static normalizeMultibyte(string $str[, array<string|int, mixed> &$map = [] ]) : string

This resolves issues with levenshtein distances being artificially larger than intended.

The results are hardly useful or even readable on their own. Replacement characters are not representative of the originals but produce more accurate results for both levenshtein and similar_text.

Note this can only handle up to 128 unique multi-byte characters until it deteriorates to the original behaviour.

https://www.php.net/manual/en/function.levenshtein.php#113702

Parameters
$str : string
$map : array<string|int, mixed> = []
Return values
string

similar()

public static similar(string $str1, string $str2[, int $flags = self::NORMALIZE_ALL ]) : bool
Parameters
$str1 : string
$str2 : string
$flags : int = self::NORMALIZE_ALL

one of the normalisation flags

Return values
bool

similarity()

Get the similarity between two strings.

public static similarity(string $str1, string $str2[, int $flags = self::NORMALIZE_ALL ]) : float
Parameters
$str1 : string
$str2 : string
$flags : int = self::NORMALIZE_ALL

one of the normalisation flags

Return values
float —

0-100 bigger is better

startsWith()

public static startsWith(string $needle, array<string|int, mixed> $haystack[, int $max = 5 ][, int $flags = self::NORMALIZE_ALL ]) : array<string|int, mixed>
Parameters
$needle : string
$haystack : array<string|int, mixed>
$max : int = 5
$flags : int = self::NORMALIZE_ALL
Return values
array<string|int, mixed>
On this page

Search results