1
|
|
|
<?php namespace BuildR\Utils; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Utility function for string analysis |
5
|
|
|
* |
6
|
|
|
* BuildR PHP Framework |
7
|
|
|
* |
8
|
|
|
* @author Zoltán Borsos <[email protected]> |
9
|
|
|
* @package Utils |
10
|
|
|
* |
11
|
|
|
* @copyright Copyright 2016, Zoltán Borsos. |
12
|
|
|
* @license https://github.com/BuildrPHP/Utils/blob/master/LICENSE.md |
13
|
|
|
* @link https://github.com/BuildrPHP/Utils |
14
|
|
|
*/ |
15
|
|
|
class StringUtils { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Determines that the given string starts with the |
19
|
|
|
* given substring |
20
|
|
|
* |
21
|
|
|
* @param string $input |
22
|
|
|
* @param string $match |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
5 |
|
public static function startsWith($input, $match) { |
27
|
5 |
|
return ($match !== '' && mb_strpos($input, $match) === 0); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Determines that the given string ends with the |
32
|
|
|
* given substring |
33
|
|
|
* |
34
|
|
|
* @param string $input |
35
|
|
|
* @param string $match |
36
|
|
|
* |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
5 |
|
public static function endsWith($input, $match) { |
40
|
5 |
|
return ((string) $match === mb_substr($input, (mb_strlen($match) * -1))); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Determines that the input string contains the |
45
|
|
|
* given substring. |
46
|
|
|
* |
47
|
|
|
* @param string $input |
48
|
|
|
* @param string $match |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
7 |
|
public static function contains($input, $match) { |
53
|
7 |
|
return (mb_strpos($input, $match, 0) !== FALSE); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* At this time PHP MBString extension does not have a mulibyte version |
58
|
|
|
* of the PHP's ucfirst() method. This is a simple implementation. |
59
|
|
|
* |
60
|
|
|
* @param string $input |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
10 |
|
public static function multiByteUcfirst($input) { |
65
|
10 |
|
$firstChar = mb_strtoupper(mb_substr($input, 0, 1)); |
66
|
10 |
|
$remaining = mb_substr($input, 1, mb_strlen($input)); |
67
|
|
|
|
68
|
10 |
|
return $firstChar . $remaining; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
*At this time PHP MBString extension does not have a mulibyte version |
73
|
|
|
* of the PHP's ucwords() method. This is a simple implementation. |
74
|
|
|
* |
75
|
|
|
* This function has a very big limitation against PHP native implementation, |
76
|
|
|
* this function only split string trough spaces instead of any whitespace |
77
|
|
|
* character |
78
|
|
|
* |
79
|
|
|
* @param $input |
80
|
|
|
*/ |
81
|
4 |
|
public static function multiByteUcwords($input) { |
82
|
4 |
|
$parts = explode(' ', $input); |
83
|
4 |
|
$result = array_map(self::class . '::multiByteUcfirst', $parts); |
84
|
|
|
|
85
|
4 |
|
return implode(' ', $result); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|