[UTIL] Fix remove affix utilities, so they only try to remove an affix if the string starts/ends with it

This commit is contained in:
Hugo Sales 2021-04-28 15:00:04 +00:00
parent 8988d89192
commit 708a910870
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -133,14 +133,20 @@ abstract class Formatting
}); });
} }
/**
* If $haystack starts with $needle, remove it from the beginning
*/
public static function removePrefix(string $haystack, string $needle) public static function removePrefix(string $haystack, string $needle)
{ {
return substr($haystack, strlen($needle)); return self::startsWith($haystack, $needle) ? substr($haystack, strlen($needle)) : $haystack;
} }
/**
* If $haystack ends with $needle, remove it from the end
*/
public static function removeSuffix(string $haystack, string $needle) public static function removeSuffix(string $haystack, string $needle)
{ {
return substr($haystack, -strlen($needle)); return self::startsWith($haystack, $needle) ? substr($haystack, -strlen($needle)) : $haystack;
} }
public static function camelCaseToSnakeCase(string $str): string public static function camelCaseToSnakeCase(string $str): string