@@ -95,4 +95,134 @@ public static function truncate(string $text, int $maxLength = 100): string {
9595 public static function removeSpecialChars (string $ text ): string {
9696 return preg_replace ('/[^a-zA-Z0-9\s]/ ' , '' , $ text );
9797 }
98+
99+ /**
100+ * Convert camelCase to snake_case.
101+ * @param string $text The input text in camelCase.
102+ * @return string The converted text in snake_case.
103+ */
104+ public static function camelToSnake (string $ text ): string {
105+ return strtolower (preg_replace ('/(?<!^)[A-Z]/ ' , '_$0 ' , $ text ));
106+ }
107+
108+ /**
109+ * Convert snake_case to camelCase.
110+ * @param string $text The input text in snake_case.
111+ * @return string The converted text in camelCase.
112+ */
113+ public static function snakeToCamel (string $ text ): string {
114+ return lcfirst (str_replace (' ' , '' , ucwords (str_replace ('_ ' , ' ' , $ text ))));
115+ }
116+
117+ /**
118+ * Convert color codes from & to § (Minecraft format).
119+ * @param string $text The input text with & color codes.
120+ * @return string The text with § color codes.
121+ */
122+ public static function colorize (string $ text ): string {
123+ return str_replace ('& ' , '§ ' , $ text );
124+ }
125+
126+ /**
127+ * Convert text to slug format (URL-friendly).
128+ * @param string $text The input text.
129+ * @param string $separator The separator to use (default: '-').
130+ * @return string The slugified text.
131+ */
132+ public static function slugify (string $ text , string $ separator = '- ' ): string {
133+ $ text = self ::removeSpecialChars ($ text );
134+ $ text = self ::removeExtraSpaces ($ text );
135+ $ text = strtolower (str_replace (' ' , $ separator , $ text ));
136+ return preg_replace ('/ ' . preg_quote ($ separator , '/ ' ) . '+/ ' , $ separator , $ text );
137+ }
138+
139+ /**
140+ * Convert camelCase to kebab-case.
141+ * @param string $text The input text in camelCase.
142+ * @return string The converted text in kebab-case.
143+ */
144+ public static function camelToKebab (string $ text ): string {
145+ return str_replace ('_ ' , '- ' , self ::camelToSnake ($ text ));
146+ }
147+
148+ /**
149+ * Convert kebab-case to camelCase.
150+ * @param string $text The input text in kebab-case.
151+ * @return string The converted text in camelCase.
152+ */
153+ public static function kebabToCamel (string $ text ): string {
154+ return self ::snakeToCamel (str_replace ('- ' , '_ ' , $ text ));
155+ }
156+
157+ /**
158+ * Capitalize the first letter of each word.
159+ * @param string $text The input text.
160+ * @return string The title case text.
161+ */
162+ public static function titleCase (string $ text ): string {
163+ return ucwords (strtolower ($ text ));
164+ }
165+
166+ /**
167+ * Capitalize only the first letter of the text.
168+ * @param string $text The input text.
169+ * @return string The capitalized text.
170+ */
171+ public static function capitalize (string $ text ): string {
172+ return ucfirst (strtolower ($ text ));
173+ }
174+
175+ /**
176+ * Reverse the text.
177+ * @param string $text The input text.
178+ * @return string The reversed text.
179+ */
180+ public static function reverse (string $ text ): string {
181+ return strrev ($ text );
182+ }
183+
184+ /**
185+ * Check if text contains a substring (case-insensitive).
186+ * @param string $text The input text.
187+ * @param string $substring The substring to search for.
188+ * @return bool True if the substring is found.
189+ */
190+ public static function contains (string $ text , string $ substring ): bool {
191+ return stripos ($ text , $ substring ) !== false ;
192+ }
193+
194+ /**
195+ * Check if text starts with a substring (case-insensitive).
196+ * @param string $text The input text.
197+ * @param string $substring The substring to check.
198+ * @return bool True if the text starts with the substring.
199+ */
200+ public static function startsWith (string $ text , string $ substring ): bool {
201+ return stripos ($ text , $ substring ) === 0 ;
202+ }
203+
204+ /**
205+ * Check if text ends with a substring (case-insensitive).
206+ * @param string $text The input text.
207+ * @param string $substring The substring to check.
208+ * @return bool True if the text ends with the substring.
209+ */
210+ public static function endsWith (string $ text , string $ substring ): bool {
211+ $ length = strlen ($ substring );
212+ if ($ length === 0 ) {
213+ return true ;
214+ }
215+ return stripos ($ text , $ substring , - $ length ) !== false ;
216+ }
217+
218+ /**
219+ * Extract a substring from the text.
220+ * @param string $text The input text.
221+ * @param int $start The starting position.
222+ * @param int|null $length The length of the substring (null for rest of string).
223+ * @return string The extracted substring.
224+ */
225+ public static function substring (string $ text , int $ start , ?int $ length = null ): string {
226+ return $ length === null ? substr ($ text , $ start ) : substr ($ text , $ start , $ length );
227+ }
98228}
0 commit comments