Text
in package
String functions
Tags
Table of Contents
- cut() : string
- Truncates a string to the specified length.
- formatFileSize() : string
- Converts the size of a file from bytes to KB, MB, etc.
- generateRandomString() : string
- Generates a random string of specified length using the given characters.
- highlighter() : string
- HTML highlighter function.
- htmlspecialchars() : string
- Converts special characters to HTML entities.
- oneLine() : string
- Returns a one-line version of the given text, with optional length limit.
- password() : string
- Returns a password-safe version of the given text.
- removeNonCharacters() : string
- Removes non-character bytes from the given text.
- repeat() : string
- Repeats a string a specified number of times.
- replace() : string
- Replaces keys in a string with corresponding values.
- toEditor() : string
- Convert special characters to their HTML entities for editor display.
- topic() : string
- Clean and format a topic text.
- unhtmlspecialchars() : string
- Convert HTML entities back to their corresponding characters.
- url() : string
- Sanitize a URL string.
- username() : string
- Sanitize a username string.
Methods
cut()
Truncates a string to the specified length.
public
static cut(string $source, int $len) : string
If the source string is longer than the specified length, it will be truncated and '...' will be appended.
Parameters
- $source : string
-
The source string
- $len : int
-
The desired length of the string (including the '...')
Tags
Return values
string —formatFileSize()
Converts the size of a file from bytes to KB, MB, etc.
public
static formatFileSize(int $bytes[, int $precision = 2 ]) : string
Returns the file size as a string in KB, MB, etc.
Parameters
- $bytes : int
-
The file size in bytes
- $precision : int = 2
-
The number of decimal places (default 2)
Tags
Return values
string —generateRandomString()
Generates a random string of specified length using the given characters.
public
static generateRandomString([int $length = 4 ][, string $characters = '0123456789' ]) : string
Parameters
- $length : int = 4
-
The length of the generated string.
- $characters : string = '0123456789'
-
The characters to use for generating the random string.
Return values
string —The randomly generated string.
highlighter()
HTML highlighter function.
public
static highlighter(string $detail) : string
Converts BBCode. Converts URLs to links.
Parameters
- $detail : string
-
The input text
Return values
string —htmlspecialchars()
Converts special characters to HTML entities.
public
static htmlspecialchars(string $text[, bool $double_encode = true ]) : string
This function replaces special characters like "&", "<", ">", etc. with their corresponding HTML entities.
Parameters
- $text : string
-
The input text
- $double_encode : bool = true
-
Whether to double encode existing entities (default true)
Tags
Return values
string —oneLine()
Returns a one-line version of the given text, with optional length limit.
public
static oneLine(string $text, int $len) : string
This function removes any leading/trailing whitespace, line breaks, tabs, and multiple spaces, and then optionally cuts the text to the specified length.
Parameters
- $text : string
-
The input text
- $len : int
-
The maximum length of the one-line text (default 0, no limit)
Tags
Return values
string —password()
Returns a password-safe version of the given text.
public
static password(string $text) : string
This function removes any characters that are not word characters, along with specific allowed characters (@, #, *, $, &, {, }, !, ?, +, _, -, =, ., [, ], ก-ฮ).
Parameters
- $text : string
-
The input text
Tags
Return values
string —removeNonCharacters()
Removes non-character bytes from the given text.
public
static removeNonCharacters(string $text) : string
This function uses a regular expression to match and remove any bytes that are not valid UTF-8 characters.
Parameters
- $text : string
-
The input text
Tags
Return values
string —repeat()
Repeats a string a specified number of times.
public
static repeat(string $text, int $count) : string
Parameters
- $text : string
-
The string to repeat
- $count : int
-
The number of times to repeat the string
Tags
Return values
string —The repeated string
replace()
Replaces keys in a string with corresponding values.
public
static replace(string $source, array<string|int, mixed> $replace) : string
Parameters
- $source : string
-
The source string to replace keys in
- $replace : array<string|int, mixed>
-
An associative array of keys and values
Tags
Return values
string —The modified string with replaced keys
toEditor()
Convert special characters to their HTML entities for editor display.
public
static toEditor(string $text) : string
Parameters
- $text : string
-
The input text
Tags
Return values
string —The text with special characters converted to HTML entities
topic()
Clean and format a topic text.
public
static topic(string $text[, bool $double_encode = true ]) : string
Parameters
- $text : string
-
The input text
- $double_encode : bool = true
-
Whether to double encode special characters (default: true)
Tags
Return values
string —The cleaned and formatted topic text
unhtmlspecialchars()
Convert HTML entities back to their corresponding characters.
public
static unhtmlspecialchars(string $text) : string
Parameters
- $text : string
-
The input text
Tags
Return values
string —The text with HTML entities converted back to characters
url()
Sanitize a URL string.
public
static url(string $text) : string
Parameters
- $text : string
-
The input URL string
Tags
Return values
string —The sanitized URL string
username()
Sanitize a username string.
public
static username(string $text) : string
Parameters
- $text : string
-
The input username string
Tags
Return values
string —The sanitized username string