1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of Rivescript-php |
4
|
|
|
* |
5
|
|
|
* (c) Shea Lewis <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Axiom\Rivescript\Support; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Str class |
15
|
|
|
* |
16
|
|
|
* A collection of string helpers. |
17
|
|
|
* |
18
|
|
|
* PHP version 7.4 and higher. |
19
|
|
|
* |
20
|
|
|
* @category Core |
21
|
|
|
* @package Support |
22
|
|
|
* @author Shea Lewis <[email protected]> |
23
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
24
|
|
|
* @link https://github.com/axiom-labs/rivescript-php |
25
|
|
|
* @since 0.3.0 |
26
|
|
|
*/ |
27
|
|
|
class Str |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Trim leading and trailing whitespace as well as |
31
|
|
|
* whitespace surrounding individual arguments. |
32
|
|
|
* |
33
|
|
|
* @param string $string The string to remove whitespace from. |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public static function removeWhitespace(string $string): string |
38
|
|
|
{ |
39
|
|
|
return preg_replace('/[\pC\pZ]+/u', ' ', trim($string)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Determine if string starts with the supplied needle. |
44
|
|
|
* |
45
|
|
|
* @param string $haystack The containing string. |
46
|
|
|
* @param string $needle The needle to look for. |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public static function startsWith(string $haystack, string $needle): bool |
51
|
|
|
{ |
52
|
|
|
return $needle === '' or mb_strrpos($haystack, $needle, -mb_strlen($haystack)) !== false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Determine if string ends with the supplied needle. |
57
|
|
|
* |
58
|
|
|
* @param string $haystack |
59
|
|
|
* @param string $needle |
60
|
|
|
* |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public static function endsWith(string $haystack, string $needle): bool |
64
|
|
|
{ |
65
|
|
|
return $needle === '' or (($temp = mb_strlen($haystack) - mb_strlen($needle)) >= 0 and mb_strpos( |
66
|
|
|
$haystack, |
67
|
|
|
$needle, |
68
|
|
|
$temp |
69
|
|
|
) !== false); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|