Bearsampp
2026.7.11
Toggle main menu visibility
Loading...
Searching...
No Matches
class.util.string.php
Go to the documentation of this file.
1
<?php
2
/*
3
*
4
* * Copyright (c) 2022-2025 Bearsampp
5
* * License: GNU General Public License version 3 or later; see LICENSE.txt
6
* * Website: https://bearsampp.com
7
* * Github: https://github.com/Bearsampp
8
*
9
*/
10
26
class
UtilString
27
{
36
public
static
function
contains
($string, $search)
37
{
38
if
(!empty($string) && !empty($search)) {
39
return
stripos($string, $search) !==
false
;
40
}
41
42
return
false
;
43
}
44
53
public
static
function
startWith
($string, $search)
54
{
55
if
($string ===
null
|| $string ===
''
) {
56
return
false
;
57
}
58
59
return
(substr($string, 0, strlen($search)) === $search);
60
}
61
70
public
static
function
endWith
($string, $search)
71
{
72
$length = strlen($search);
73
$start = $length * -1;
74
75
return
(substr($string, $start) === $search);
76
}
77
85
public
static
function
isAlphanumeric
($string)
86
{
87
return
ctype_alnum($string);
88
}
89
99
public
static
function
random
($length = 32, $withNumeric =
true
)
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
}
120
130
public
static
function
generateSecureToken
($length = 32)
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
}
139
149
public
static
function
generateSecureBytes
($length = 32)
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
}
158
}
159
Log\error
static error($data, $file=null)
Definition
class.log.php:650
UtilString
Definition
class.util.string.php:27
UtilString\contains
static contains($string, $search)
Definition
class.util.string.php:36
UtilString\generateSecureToken
static generateSecureToken($length=32)
Definition
class.util.string.php:130
UtilString\isAlphanumeric
static isAlphanumeric($string)
Definition
class.util.string.php:85
UtilString\startWith
static startWith($string, $search)
Definition
class.util.string.php:53
UtilString\random
static random($length=32, $withNumeric=true)
Definition
class.util.string.php:99
UtilString\endWith
static endWith($string, $search)
Definition
class.util.string.php:70
UtilString\generateSecureBytes
static generateSecureBytes($length=32)
Definition
class.util.string.php:149
sandbox
core
classes
class.util.string.php
Generated by
1.17.0