| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Helpers; |
| 4 |
|
| 5 |
class Str |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Determine if a given string starts with a given substring. |
| 9 |
* |
| 10 |
* @param string $haystack |
| 11 |
* @param string|array $needles |
| 12 |
* |
| 13 |
* @return bool |
| 14 |
*/ |
| 15 |
public static function startsWith($haystack, $needles) |
| 16 |
{ |
| 17 |
if (is_array($haystack)) { |
| 18 |
$haystack = implode(' ', $haystack); |
| 19 |
} |
| 20 |
|
| 21 |
foreach ((array) $needles as $needle) { |
| 22 |
if ('' != $needle && substr($haystack, 0, strlen($needle)) === (string) $needle) { |
| 23 |
return true; |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Determine if a given string ends with a given substring. |
| 32 |
* |
| 33 |
* @param string $haystack |
| 34 |
* @param string|array $needles |
| 35 |
* |
| 36 |
* @return bool |
| 37 |
*/ |
| 38 |
public static function endsWith($haystack, $needles) |
| 39 |
{ |
| 40 |
if (is_array($haystack)) { |
| 41 |
$haystack = implode(' ', $haystack); |
| 42 |
} |
| 43 |
|
| 44 |
foreach ((array) $needles as $needle) { |
| 45 |
if (substr($haystack, -strlen($needle)) === (string) $needle) { |
| 46 |
return true; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
return false; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Determine if a given string contains a given substring. |
| 55 |
* |
| 56 |
* @param string $haystack |
| 57 |
* @param string|array $needles |
| 58 |
* |
| 59 |
* @return bool |
| 60 |
*/ |
| 61 |
public static function contains($haystack, $needles) |
| 62 |
{ |
| 63 |
if (is_array($haystack)) { |
| 64 |
$haystack = implode(' ', $haystack); |
| 65 |
} |
| 66 |
|
| 67 |
foreach ((array) $needles as $needle) { |
| 68 |
if ('' != $needle && false !== fluentform_mb_strpos(strtolower($haystack), strtolower($needle))) { |
| 69 |
return true; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Determine if a given string does not contain a given substring. |
| 78 |
* |
| 79 |
* @param string $haystack |
| 80 |
* @param string|array $needles |
| 81 |
* |
| 82 |
* @return bool |
| 83 |
*/ |
| 84 |
public static function doNotContains($haystack, $needles) |
| 85 |
{ |
| 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; |
| 110 |
} |
| 111 |
} |
| 112 |
|