allowing arrays and string

This commit is contained in:
Dennis Eichhorn 2016-05-27 16:49:18 +02:00
parent d0222c7709
commit 8c79ec1a86

View File

@ -42,65 +42,105 @@ class StringUtils
/** /**
* String ends with? * String ends with?
* *
* @param string $haystack Haystack * @param string $haystack Haystack
* @param string $needle Needle * @param string|array $needles Needles
* *
* @return bool * @return bool
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn * @author Dennis Eichhorn
*/ */
public static function endsWith(string $haystack, string $needle) : bool public static function endsWith(string $haystack, $needles) : bool
{ {
return $needle === '' || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false); if (is_string($needles)) {
self::endsWith($haystack, [$needles]);
}
foreach ($needles as $needle) {
if ($needle === '' || (($temp = strlen($haystack) - strlen($needle)) >= 0 && strpos($haystack, $needle, $temp) !== false)) {
return true;
}
}
return false;
} }
/** /**
* String starts with? * String starts with?
* *
* @param string $haystack Haystack * @param string $haystack Haystack
* @param string $needle Needle * @param string|array $needles Needles
* *
* @return bool * @return bool
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn * @author Dennis Eichhorn
*/ */
public static function startsWith(string $haystack, string $needle) : bool public static function startsWith(string $haystack, $needles) : bool
{ {
return $needle === '' || strrpos($haystack, $needle, -strlen($haystack)) !== false; if (is_string($needles)) {
self::startsWith($haystack, [$needles]);
}
foreach ($needles as $needle) {
if ($needle === '' || strrpos($haystack, $needle, -strlen($haystack)) !== false) {
return true;
}
}
return false;
} }
/** /**
* String starts with? * String starts with?
* *
* @param string $haystack Haystack * @param string $haystack Haystack
* @param string $needle Needle * @param string|array $needles Needles
* *
* @return bool * @return bool
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn * @author Dennis Eichhorn
*/ */
public static function mb_startsWith(string $haystack, string $needle) : bool public static function mb_startsWith(string $haystack, $needles) : bool
{ {
return $needle === '' || mb_strrpos($haystack, $needle, -mb_strlen($haystack)) !== false; if (is_string($needles)) {
self::mb_startsWith($haystack, [$needles]);
}
foreach ($needles as $needle) {
if ($needle === '' || mb_strrpos($haystack, $needle, -mb_strlen($haystack)) !== false) {
return true;
}
}
return false;
} }
/** /**
* String ends with? * String ends with?
* *
* @param string $haystack Haystack * @param string $haystack Haystack
* @param string $needle Needle * @param string|array $needles Needles
* *
* @return bool * @return bool
* *
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn * @author Dennis Eichhorn
*/ */
public static function mb_endsWith(string $haystack, string $needle) : bool public static function mb_endsWith(string $haystack, $needles) : bool
{ {
return $needle === '' || (($temp = mb_strlen($haystack) - mb_strlen($needle)) >= 0 && mb_strpos($haystack, $needle, $temp) !== false); if (is_string($needles)) {
self::mb_endsWith($haystack, [$needles]);
}
foreach ($needles as $needle) {
if ($needle === '' || (($temp = mb_strlen($haystack) - mb_strlen($needle)) >= 0 && mb_strpos($haystack, $needle, $temp) !== false)) {
return true;
}
}
return false;
} }
/** /**