[FORMATTING] Add utilities to remove affixes from strings

This commit is contained in:
Hugo Sales 2021-04-27 20:52:12 +00:00
parent b647e31495
commit 60a9085e56
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
2 changed files with 13 additions and 3 deletions

View File

@ -237,16 +237,16 @@ abstract class Common
} }
/** /**
* If $secure is true, only allow https URLs to pass * If $ensure_secure is true, only allow https URLs to pass
*/ */
public function isValidHttpUrl(string $url, bool $ensure_secure = false) public static function isValidHttpUrl(string $url, bool $ensure_secure = false)
{ {
if (empty($url)) { if (empty($url)) {
return false; return false;
} }
// (if false, we use '?' in 'https?' to say the 's' is optional) // (if false, we use '?' in 'https?' to say the 's' is optional)
$regex = $secure ? '/^https$/' : '/^https?$/'; $regex = $ensure_secure ? '/^https$/' : '/^https?$/';
return filter_var($url, FILTER_VALIDATE_URL) return filter_var($url, FILTER_VALIDATE_URL)
&& preg_match($regex, parse_url($url, PHP_URL_SCHEME)); && preg_match($regex, parse_url($url, PHP_URL_SCHEME));
} }

View File

@ -133,6 +133,16 @@ abstract class Formatting
}); });
} }
public static function removePrefix(string $haystack, string $needle)
{
return substr($haystack, strlen($needle));
}
public static function removeSuffix(string $haystack, string $needle)
{
return substr($haystack, -strlen($needle));
}
public static function camelCaseToSnakeCase(string $str): string public static function camelCaseToSnakeCase(string $str): string
{ {
return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $str)); return strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $str));