You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
5.4 KiB
167 lines
5.4 KiB
<?php
|
|
|
|
declare(strict_types = 1);
|
|
|
|
/**
|
|
* @author Robert Strutts
|
|
* @copyright (c) 2026, Robert Strutts
|
|
* @license MIT
|
|
*/
|
|
namespace IOcornerstone\Framework\String;
|
|
|
|
/**
|
|
* Description of mbStringFns
|
|
*
|
|
* @author Robert Strutts
|
|
*/
|
|
class mbStringFns
|
|
{
|
|
public static function isUTF8(string $string) {
|
|
return mb_check_encoding($string, 'UTF-8');
|
|
}
|
|
|
|
// Check if string contains multibyte characters
|
|
public static function hasMultibyteChars(string $string) {
|
|
return strlen($string) !== mb_strlen($string, 'UTF-8');
|
|
}
|
|
|
|
public static function strContains(string $haystack, string $needle) {
|
|
return $needle !== '' && mb_strpos($haystack, $needle) !== false;
|
|
}
|
|
|
|
// Return character by Unicode code point value
|
|
public static function chr(int $codepoint, ?string $encoding = null): string|false {
|
|
return mb_chr($codepoint, $encoding);
|
|
}
|
|
|
|
// Parse GET/POST/COOKIE data and set global variable
|
|
public static function parseStr(string $string, array &$result): bool {
|
|
return mb_parse_str($string, $result);
|
|
}
|
|
|
|
// Strip whitespace (or other characters) from the end of a string
|
|
public static function rtrim(string $string, ?string $characters = null, ?string $encoding = null): string {
|
|
return mb_rtrim($string, $characters, $encoding);
|
|
}
|
|
|
|
// Strip whitespace (or other characters) from the beginning of a string
|
|
public static function ltrim(string $string, ?string $characters = null, ?string $encoding = null): string {
|
|
return mb_ltrim($string, $characters, $encoding);
|
|
}
|
|
|
|
// Finds position of first occurrence of a string within another, case insensitive
|
|
public static function stripos(
|
|
string $haystack,
|
|
string $needle,
|
|
int $offset = 0,
|
|
?string $encoding = null
|
|
): int|false {
|
|
return mb_stripos($haystack, $needle, $offset, $encoding);
|
|
}
|
|
|
|
// Finds first occurrence of a string within another, case insensitive
|
|
public static function stristr(
|
|
string $haystack,
|
|
string $needle,
|
|
bool $before_needle = false,
|
|
?string $encoding = null
|
|
): string|false {
|
|
return mb_stristr($haystack, $needle, $before_needle, $encoding);
|
|
}
|
|
|
|
// Find position of first occurrence of string in a string
|
|
public static function strpos(
|
|
string $haystack,
|
|
string $needle,
|
|
int $offset = 0,
|
|
?string $encoding = null
|
|
): int|false {
|
|
return mb_strpos($haystack, $needle, $offset, $encoding);
|
|
}
|
|
|
|
// Finds the last occurrence of a character in a string within another
|
|
public static function strrchr(
|
|
string $haystack,
|
|
string $needle,
|
|
bool $before_needle = false,
|
|
?string $encoding = null
|
|
): string|false {
|
|
return mb_strrchr($haystack, $needle, $before_needle, $encoding);
|
|
}
|
|
|
|
// Finds the last occurrence of a character in a string within another, case insensitive
|
|
public static function strrichr(
|
|
string $haystack,
|
|
string $needle,
|
|
bool $before_needle = false,
|
|
?string $encoding = null
|
|
): string|false {
|
|
return mb_strrichr($haystack, $needle, $before_needle, $encoding);
|
|
}
|
|
|
|
// Finds position of last occurrence of a string within another, case insensitive
|
|
public static function strripos(
|
|
string $haystack,
|
|
string $needle,
|
|
int $offset = 0,
|
|
?string $encoding = null
|
|
): int|false {
|
|
return mb_strripos($haystack, $needle, $offset, $encoding);
|
|
}
|
|
|
|
// Find position of last occurrence of a string in a string
|
|
public static function strrpos(
|
|
string $haystack,
|
|
string $needle,
|
|
int $offset = 0,
|
|
?string $encoding = null
|
|
): int|false {
|
|
return mb_strrpos($haystack, $needle, $offset, $encoding);
|
|
}
|
|
|
|
// Finds first occurrence of a string within another
|
|
public static function strstr(
|
|
string $haystack,
|
|
string $needle,
|
|
bool $before_needle = false,
|
|
?string $encoding = null
|
|
): string|false {
|
|
return mb_strstr($haystack, $needle, $before_needle, $encoding);
|
|
}
|
|
|
|
// Strip whitespace (or other characters) from the beginning and end of a string
|
|
public static function trim(string $string, ?string $characters = null, ?string $encoding = null): string {
|
|
return mb_trim($string, $characters, $encoding);
|
|
}
|
|
|
|
// Make a string's first character uppercase
|
|
public static function ucfirst(string $string, ?string $encoding = null): string {
|
|
return mb_ucfirst($string, $encoding);
|
|
}
|
|
|
|
// Override to get the length of a string with multibyte support
|
|
public static function strlen($string) {
|
|
return mb_strlen($string);
|
|
}
|
|
|
|
// Override to convert a string to lowercase with multibyte support
|
|
public static function strtolower($string) {
|
|
return mb_strtolower($string);
|
|
}
|
|
|
|
// Override to convert a string to uppercase with multibyte support
|
|
public static function strtoupper($string) {
|
|
return mb_strtoupper($string);
|
|
}
|
|
|
|
// Override to get part/substring from a string with multibyte support
|
|
public static function substr($string, $start, $length = null) {
|
|
return ($length !== null) ? mb_substr($string, $start, $length) : mb_substr($string, $start);
|
|
}
|
|
|
|
// Count the number of substring occurrences
|
|
public static function substrCount(string $haystack, string $needle, ?string $encoding = null): int {
|
|
return mb_substr_count($haystack, $needle, $encoding);
|
|
}
|
|
|
|
}
|
|
|