[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 83415b7aa6
commit e08767cec0

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