Bearsampp 2026.5.5
Loading...
Searching...
No Matches
UtilString Class Reference

Static Public Member Functions

static contains ($string, $search)
static endWith ($string, $search)
static generateSecureBytes ($length=32)
static generateSecureToken ($length=32)
static isAlphanumeric ($string)
static random ($length=32, $withNumeric=true)
static startWith ($string, $search)

Detailed Description

String utility methods and cryptographically secure random generators.

Provides case-insensitive substring checks, prefix/suffix tests, alphanumeric validation, and secure random string/token/bytes generation.

Note: This class is required directly by the bootstrap (class.root.php) because the autoloader itself depends on UtilString\startWith().

Usage:

UtilString::startWith('foobar', 'foo'); // true
UtilString::random(16); // cryptographically secure random string
static startWith($string, $search)
static random($length=32, $withNumeric=true)

Definition at line 26 of file class.util.string.php.

Member Function Documentation

◆ contains()

contains ( $string,
$search )
static

Checks if a string contains a specified substring (case-insensitive).

Parameters
string$stringThe string to search in.
string$searchThe substring to search for.
Returns
bool Returns true if the substring is found, otherwise false.

Definition at line 36 of file class.util.string.php.

37 {
38 if (!empty($string) && !empty($search)) {
39 return stripos($string, $search) !== false;
40 }
41
42 return false;
43 }

Referenced by BinMailpit\checkPort(), BinXlight\checkPort(), BinMariadb\getCmdLineOutput(), BinMysql\getCmdLineOutput(), Util\is32BitsOs(), and Win32Ps\killBins().

◆ endWith()

endWith ( $string,
$search )
static

Checks if a string ends with a specified substring.

Parameters
string$stringThe string to check.
string$searchThe substring to look for at the end of the string.
Returns
bool Returns true if the string ends with the search substring, otherwise false.

Definition at line 70 of file class.util.string.php.

71 {
72 $length = strlen($search);
73 $start = $length * -1;
74
75 return (substr($string, $start) === $search);
76 }

Referenced by Batch\exec(), Util\findFiles(), BinApache\getAlias(), BinPhp\getExtensionsFromFolder(), LangProc\getList(), TplAppLogs\getMenuLogs(), BinApache\getModulesFromFolder(), Batch\getProcessUsingPort(), and BinApache\getVhosts().

◆ generateSecureBytes()

generateSecureBytes ( $length = 32)
static

Generates a cryptographically secure random bytes string. Useful for encryption keys, initialization vectors, etc.

Parameters
int$lengthThe length in bytes.
Returns
string Returns raw binary random bytes.
Exceptions
ExceptionIf an appropriate source of randomness cannot be found.

Definition at line 149 of file class.util.string.php.

150 {
151 try {
152 return random_bytes($length);
153 } catch (Exception $e) {
154 Log::error('Failed to generate secure bytes: ' . $e->getMessage());
155 throw $e;
156 }
157 }
static error($data, $file=null)

References Log\error().

◆ generateSecureToken()

generateSecureToken ( $length = 32)
static

Generates a cryptographically secure random token as a hexadecimal string. Ideal for security tokens, session IDs, CSRF tokens, etc.

Parameters
int$lengthThe length in bytes (output will be double this in hex characters).
Returns
string Returns a hexadecimal string of cryptographically secure random bytes.
Exceptions
ExceptionIf an appropriate source of randomness cannot be found.

Definition at line 130 of file class.util.string.php.

131 {
132 try {
133 return bin2hex(random_bytes($length));
134 } catch (Exception $e) {
135 Log::error('Failed to generate secure token: ' . $e->getMessage());
136 throw $e;
137 }
138 }

References Log\error().

◆ isAlphanumeric()

isAlphanumeric ( $string)
static

Checks if a string is alphanumeric.

Parameters
string$stringThe string to check.
Returns
bool Returns true if the string is alphanumeric, false otherwise.

Definition at line 85 of file class.util.string.php.

86 {
87 return ctype_alnum($string);
88 }

◆ random()

random ( $length = 32,
$withNumeric = true )
static

Generates a cryptographically secure random string of specified length and character set.

Parameters
int$lengthThe length of the random string to generate.
bool$withNumericWhether to include numeric characters in the random string.
Returns
string Returns the generated random string.
Exceptions
ExceptionIf an appropriate source of randomness cannot be found.

Definition at line 99 of file class.util.string.php.

100 {
101 $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
102 if ($withNumeric) {
103 $characters .= '0123456789';
104 }
105
106 $charactersLength = strlen($characters);
107 $randomString = '';
108
109 try {
110 for ($i = 0; $i < $length; $i++) {
111 $randomString .= $characters[random_int(0, $charactersLength - 1)];
112 }
113 } catch (Exception $e) {
114 Log::error('Failed to generate cryptographically secure random string: ' . $e->getMessage());
115 throw $e;
116 }
117
118 return $randomString;
119 }

References Log\error().

Referenced by OpenSsl\createCrt(), and Batch\getTmpFile().

◆ startWith()

startWith ( $string,
$search )
static

Checks if a string starts with a specified substring.

Parameters
string$stringThe string to check.
string$searchThe substring to look for at the start of the string.
Returns
bool Returns true if the string starts with the search substring, otherwise false.

Definition at line 53 of file class.util.string.php.

54 {
55 if ($string === null || $string === '') {
56 return false;
57 }
58
59 return (substr($string, 0, strlen($search)) === $search);
60 }

Referenced by BinApache\checkPort(), BinMariadb\checkPort(), BinMysql\checkPort(), Util\findFiles(), BinApache\getModulesFromConf(), BinApache\getModulesFromFolder(), BinApache\getOfflineContent(), BinApache\getOnlineContent(), Batch\getOsInfo(), Batch\getPearVersion(), Batch\getProcessUsingPort(), BinApache\getVhostsUrl(), Win32Ps\killBins(), and Autoloader\resolveClassPath().


The documentation for this class was generated from the following file: