| @@ -6,20 +6,21 @@ | ||
| 6 | 6 | { |
| 7 | 7 | /** |
| 8 | 8 | * Determine if a given string starts with a given substring. |
| 9 | 9 | * |
| 10 | - * @param string $haystack | |
| 10 | + * @param string $haystack | |
| 11 | 11 | * @param string|array $needles |
| 12 | + * | |
| 12 | 13 | * @return bool |
| 13 | 14 | */ |
| 14 | 15 | public static function startsWith($haystack, $needles) |
| 15 | 16 | { |
| 16 | - if(is_array($haystack)) { | |
| 17 | + if (is_array($haystack)) { | |
| 17 | 18 | $haystack = implode(' ', $haystack); |
| 18 | 19 | } |
| 19 | 20 | |
| 20 | 21 | foreach ((array) $needles as $needle) { |
| 21 | - if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) { | |
| 22 | + if ('' != $needle && substr($haystack, 0, strlen($needle)) === (string) $needle) { | |
| 22 | 23 | return true; |
| 23 | 24 | } |
| 24 | 25 | } |
| 25 | 26 | |
| @@ -28,19 +29,21 @@ | ||
| 28 | 29 | |
| 29 | 30 | /** |
| 30 | 31 | * Determine if a given string ends with a given substring. |
| 31 | 32 | * |
| 32 | - * @param string $haystack | |
| 33 | + * @param string $haystack | |
| 33 | 34 | * @param string|array $needles |
| 35 | + * | |
| 34 | 36 | * @return bool |
| 35 | 37 | */ |
| 36 | 38 | public static function endsWith($haystack, $needles) |
| 37 | 39 | { |
| 38 | - if(is_array($haystack)) { | |
| 40 | + if (is_array($haystack)) { | |
| 39 | 41 | $haystack = implode(' ', $haystack); |
| 40 | 42 | } |
| 43 | + | |
| 41 | 44 | foreach ((array) $needles as $needle) { |
| 42 | - if (substr($haystack, - strlen($needle)) === (string) $needle) { | |
| 45 | + if (substr($haystack, -strlen($needle)) === (string) $needle) { | |
| 43 | 46 | return true; |
| 44 | 47 | } |
| 45 | 48 | } |
| 46 | 49 | |
| @@ -49,20 +52,21 @@ | ||
| 49 | 52 | |
| 50 | 53 | /** |
| 51 | 54 | * Determine if a given string contains a given substring. |
| 52 | 55 | * |
| 53 | - * @param string $haystack | |
| 56 | + * @param string $haystack | |
| 54 | 57 | * @param string|array $needles |
| 58 | + * | |
| 55 | 59 | * @return bool |
| 56 | 60 | */ |
| 57 | 61 | public static function contains($haystack, $needles) |
| 58 | 62 | { |
| 59 | - if(is_array($haystack)) { | |
| 63 | + if (is_array($haystack)) { | |
| 60 | 64 | $haystack = implode(' ', $haystack); |
| 61 | 65 | } |
| 62 | 66 | |
| 63 | 67 | foreach ((array) $needles as $needle) { |
| 64 | - if ($needle != '' && fluentform_mb_strpos($haystack, $needle) !== false) { | |
| 68 | + if ('' != $needle && false !== fluentform_mb_strpos(strtolower($haystack), strtolower($needle))) { | |
| 65 | 69 | return true; |
| 66 | 70 | } |
| 67 | 71 | } |
| 68 | 72 | |
| @@ -71,13 +75,37 @@ | ||
| 71 | 75 | |
| 72 | 76 | /** |
| 73 | 77 | * Determine if a given string does not contain a given substring. |
| 74 | 78 | * |
| 75 | - * @param string $haystack | |
| 79 | + * @param string $haystack | |
| 76 | 80 | * @param string|array $needles |
| 81 | + * | |
| 77 | 82 | * @return bool |
| 78 | 83 | */ |
| 79 | 84 | public static function doNotContains($haystack, $needles) |
| 80 | 85 | { |
| 81 | 86 | return !self::contains($haystack, $needles); |
| 87 | + } | |
| 88 | + | |
| 89 | + /** | |
| 90 | + * Split string as array of string on given substring. | |
| 91 | + * | |
| 92 | + * @param string $haystack | |
| 93 | + * @param string|array $needles | |
| 94 | + * | |
| 95 | + * @return array | |
| 96 | + */ | |
| 97 | + public static function separateString($haystack, $needles) | |
| 98 | + { | |
| 99 | + $separateArray = []; | |
| 100 | + if (self::contains($haystack, $needles)) { | |
| 101 | + if (is_array($needles)) { | |
| 102 | + foreach ($needles as $needle) { | |
| 103 | + $separateArray[] = array_map('trim', explode($needle, $haystack)); | |
| 104 | + } | |
| 105 | + } else { | |
| 106 | + $separateArray = array_map('trim', explode($needles, $haystack)); | |
| 107 | + } | |
| 108 | + } | |
| 109 | + return $separateArray; | |
| 82 | 110 | } |
| 83 | 111 | } |