Text helper
1Presentation
Helper offering several methods of handling character strings.
2filenamize()
Converts text into a string that can be used as a file name.
Method signature:
filenamize(string $filename, bool $hyphenSpaces=true, bool $lowercase=true) : string
Parameters:
- $filename: Text to convert.
- $hyphenSpaces: true to replace spaces with dashes.
- $lowercase: true to make text all lowercase.
Return value: The file name.
3urlize()
Converts text so that it can be used in a URL.
Method signature:
urlize(?string $txt, bool $avoidUnderscores=true) : string
Parameters:
- $txt: Text to convert.
- $avoidUnderscores: true to replace underscores with dashes.
Return value: The converted text.
4isValidHtmlSyntax()
This method checks whether an HTML feed is syntactically correct.
Method signature:
isValidHtmlSyntax(string $html) : bool
Parameter: The HTML stream to be validated.
Return value: true if the HTML stream is valid, false otherwise.
5htmlToText()
This method transforms an HTML stream into plain text.
Method signature:
htmlToText(string $html, bool $cleanup=false) : string
Parameters:
- $html: HTML stream to convert.
- $cleanup: true to remove the content of <blockquote>, <pre> and <code> tags.
Return value: Plain text generated from HTML.
6encodingCompatible()
This method indicates whether a character string encoded in UTF-8 contains only characters compatible with the encoding supplied as a parameter.
Method signature:
encodingCompatible(string $text, string $encoding) : bool
Parameters:
- $text: The text to analyze.
- $encoding: The encoding to use.
Return value: Return value : True if the text contains only characters compatible with the character encoding supplied in parameter.
7ascii()
This method converts a string so that it contains only ASCII characters.
Method signature:
ascii(?string $text) : string
Parameters:
- $text : The text to be transformed.
Return value: The transformed text.
8firstChunk()
This method returns the part of a string before a separator.
Method signature:
firstChunk(string $str, string $separator) : string
Parameters:
- $str : The input string.
- $separator : The separator.
Return value: The part before the string, or an empty string if the separator was not found.
9lastChunk()
This method returns the part of a string after a separator.
Method signature:
lastChunk(string $str, string $separator) : string
Parameters:
- $str : The input string
- $separator : The separator.
Return value: The part after the string, or an empty string if the separator was not found.
10hasLower()
This method indicates whether a string contains a lowercase character.
Method signature:
hasLower(?string $txt) : bool
Parameters:
- $txt : The string to be analyzed.
Return value : true if the string contains a lowercase character. False otherwise.
11hasUpper()
This method indicates whether a string contains an uppercase character.
Method signature:
hasUpper(?string $txt) : bool
Parameters:
- $txt : The string to be analyzed.
Return value: true if the string contains an uppercase character. False otherwise.
12convertCase()
This method converts the writing style of a character string.
Method signature:
convertCase(?string $txt, string $inCase, string $outCase, ?bool $upperCase=null, bool $ascii=false) : ?string
Parameters:
- $txt : The string to convert.
- $inCase : The writing style of the input string. Can take the values \Temma\Utils\Text::SNAKE_CASE, \Temma\Utils\Text::KEBAB_CASE, \Temma\Utils\Text::CAMEL_CASE or \Temma\Utils\Text::PASCAL_CASE.
- $outCase : The writing style of the output string. Can take the values \Temma\Utils\Text::SNAKE_CASE, \Temma\Utils\Text::KEBAB_CASE, \Temma\Utils\Text::CAMEL_CASE or \Temma\Utils\Text::PASCAL_CASE.
- $upperCase : (optional) true for uppercase, false for lowercase, null for case insensitive.
- $ascii : true to convert the output string to contain only ASCII characters.
Return value: The converted string, or null if the input string was null.
13mimeTypesMatch()
This method checks whether two MIME types are compatible. Both MIME types must have the same upper/lower case.
Method signature:
mimeTypesMatch(string|array $mime1, string|array $mime2) : bool
Parameters:
- $mime1: First MIME type. String in the form "image/png", "image/*" or "image". Array in the form ['image', 'png'], ['image', '*'] or ['image'].
- $mime2: Second MIME type (same formats as $mime1).
Return value: true if the MIME types are compatible.
Usage example:
use \Temma\Utils\Text as TµText;
// exact match
TµText::mimeTypesMatch('image/png', 'image/png'); // true
// wildcard match
TµText::mimeTypesMatch('image/*', 'image/png'); // true
// without subtype (equivalent to "image/*")
TµText::mimeTypesMatch('image', 'image/jpeg'); // true
// incompatible types
TµText::mimeTypesMatch('image/png', 'text/html'); // false
// with arrays
TµText::mimeTypesMatch(['image', 'png'], ['image', '*']); // true
14parseSize()
This method parses a size string (e.g. '10M', '5K') and returns the corresponding value in bytes.
Method signature:
parseSize(mixed $len, int $base=1024) : ?int
Parameters:
- $len: The size value to parse. Can be an integer, a float, or a string with a unit suffix.
- $base: (optional) The desired calculation base. Default: 1024.
Return value: The size in bytes, or null if null was received as parameter.
Exception: Throws a \Temma\Exceptions\IO exception if the string is not in a valid format.
Recognized suffixes (case insensitive):
- K: kilo (base1)
- M: mega (base2)
- G: giga (base3)
- T: tera (base4)
- P: peta (base5)
- E: exa (base6)
Usage example:
use \Temma\Utils\Text as TµText;
// with default base (1024)
TµText::parseSize('10K'); // 10240
TµText::parseSize('5M'); // 5242880
TµText::parseSize('2G'); // 2147483648
// with base 1000
TµText::parseSize('10K', 1000); // 10000
TµText::parseSize('5M', 1000); // 5000000
// direct numeric values
TµText::parseSize(1024); // 1024
TµText::parseSize(null); // null