| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FirePlugins Framework |
| 4 |
* @version 1.0.21 |
| 5 |
* |
| 6 |
* @author FirePlugins <[email protected]> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2022 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FPFramework\Helpers; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class StringHelper |
| 20 |
{ |
| 21 |
/** |
| 22 |
* UTF-8 aware alternative to strpos() |
| 23 |
* |
| 24 |
* Find position of first occurrence of a string. |
| 25 |
* |
| 26 |
* @param string $str String being examined |
| 27 |
* @param string $search String being searched for |
| 28 |
* @param integer $offset Optional, specifies the position from which the search should be performed |
| 29 |
* |
| 30 |
* @return integer|boolean Number of characters before the first match or FALSE on failure |
| 31 |
*/ |
| 32 |
public static function strpos($str, $search, $offset = false) |
| 33 |
{ |
| 34 |
if ($offset === false) |
| 35 |
{ |
| 36 |
return self::utf8_strpos($str, $search); |
| 37 |
} |
| 38 |
|
| 39 |
return self::utf8_strpos($str, $search, $offset); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Assumes mbstring internal encoding is set to UTF-8 |
| 44 |
* Wrapper around mb_strpos |
| 45 |
* Find position of first occurrence of a string |
| 46 |
* |
| 47 |
* @param string haystack |
| 48 |
* @param string needle (you should validate this with utf8_is_valid) |
| 49 |
* @param integer offset in characters (from left) |
| 50 |
* |
| 51 |
* @return mixed integer position or FALSE on failure |
| 52 |
*/ |
| 53 |
public static function utf8_strpos($str, $search, $offset = FALSE) |
| 54 |
{ |
| 55 |
if ( $offset === FALSE ) { |
| 56 |
return mb_strpos($str, $search); |
| 57 |
} else { |
| 58 |
return mb_strpos($str, $search, $offset); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Checks whether $haystack starts with $needle. |
| 64 |
* |
| 65 |
* @param string $haystack |
| 66 |
* @param string $needle |
| 67 |
* |
| 68 |
* @return int |
| 69 |
*/ |
| 70 |
public static function startsWith($haystack, $needle) |
| 71 |
{ |
| 72 |
return substr_compare($haystack, $needle, 0, strlen($needle)) === 0; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Checks whether $haystack ends with $needle. |
| 77 |
* |
| 78 |
* @param string $haystack |
| 79 |
* @param string $needle |
| 80 |
* |
| 81 |
* @return int |
| 82 |
*/ |
| 83 |
public static function endsWith($haystack, $needle) |
| 84 |
{ |
| 85 |
return substr_compare($haystack, $needle, -strlen($needle)) === 0; |
| 86 |
} |
| 87 |
} |