Bearsampp 2026.5.5
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
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}
static error($data, $file=null)
static contains($string, $search)
static generateSecureToken($length=32)
static isAlphanumeric($string)
static startWith($string, $search)
static random($length=32, $withNumeric=true)
static endWith($string, $search)
static generateSecureBytes($length=32)