Kotchasan

Text
in package

String functions

Tags
see
https://www.kotchasan.com/

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
assert

('สวัสดี ประเทศไทย', 8) [==] 'สวัสดี..'

assert

('123456789', 8) [==] '123456..'

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
assert

(256) [==] '256 Bytes'

assert

(1024) [==] '1 KB'

assert

(1024 * 1024) [==] '1 MB'

assert

(1024 * 1024 * 1024) [==] '1 GB'

assert

(1024 * 1024 * 1024 * 1024) [==] '1 TB'

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
assert

('&"'<>\}$') [==] '&"'<>\{}$'

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
assert

(" \tทดสอบ\r\nภาษาไทย") [==] 'ทดสอบ ภาษาไทย'

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
assert

(" 0\n12 34\r\r6\t5ทดสอบ@#$&}!?+-=.[]*") [==] '0123465ทดสอบ@#$&}!?+-=.[]*'

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
assert

(chr(0).chr(127).chr(128).chr(255)) [==] chr(0).chr(127)

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
assert

('0', 10) [==] '0000000000'

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
assert

("SELECT * FROM table WHERE id=:id AND lang IN (:lang, '')", array(':id' => 1, array(':lang' => 'th'))) [==] "SELECT * FROM table WHERE id=1 AND lang IN (th, '')"

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
assert

('&"'."'<>}&&") [==] "&"'<>{}&&"

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
assert

(' ทด/สอบ$'."\r\n\t".' ') [==] 'ทด\/สอบ$ <?php echo '555'?>'

assert

(' ') [==] '&nbsp;'

assert

(' ', false) [==] ' '

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
assert

(\Kotchasan\Text::htmlspecialchars('&"'<>\}$')) [==] '&"'<>\}$'

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
assert

(" http://www.kotchasan.com?a=1&b=2&c=3 ") [==] 'http://www.kotchasan.com?a=1&b=2&c=3'

assert

("javascript:alert('xxx')") [==] 'alertxxx'

assert

("http://www.xxx.com/javascript/") [==] 'http://www.xxx.com/javascript/'

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
assert

(' ad_min@demo.com') [==] 'ad_min@demo.com'

assert

('012 3465') [==] '0123465'

Return values
string

The sanitized username string

Search results