forked from corpsee/php-utf-8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmbstring.php
More file actions
156 lines (139 loc) · 3.87 KB
/
Copy pathmbstring.php
File metadata and controls
156 lines (139 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
namespace utf8;
// utf8_strpos() and utf8_strrpos() need utf8_bad_strip() to strip invalid
// characters. Mbstring doesn't do this while the Native implementation does.
require_once PHP_UTF_8_DIR . '/utils/patterns.php';
require_once PHP_UTF_8_DIR . '/utils/bad.php';
/**
* Wrapper round mb_strlen.
*
* This function does not count bad bytes in the string - these are simply ignored.
*
* @param string $str UTF-8 string
*
* @return int number of UTF-8 characters in string
*/
function len($str)
{
return mb_strlen($str);
}
/**
* Wrapper around mb_strpos.
*
* Find position of first occurrence of a string.
*
* @param string $str haystack
* @param string $search needle (you should validate this with utf8_is_valid)
* @param integer|false $offset (optional) offset in characters (from left)
*
* @return mixed integer position or false on failure
*/
function pos($str, $search, $offset = false)
{
$str = bad_clean($str);
if ($offset === false) {
return mb_strpos($str, $search);
}
return mb_strpos($str, $search, $offset);
}
/**
* Wrapper around mb_strrpos.
*
* Find position of last occurrence of a char in a string.
*
* @param string $str haystack
* @param string $search needle (you should validate this with utf8_is_valid)
* @param integer|false $offset (optional) offset (from left)
*
* @return mixed integer position or false on failure
*/
function rpos($str, $search, $offset = false)
{
$str = bad_clean($str);
if (!$offset) {
// Emulate behaviour of strrpos rather than raising warning
if (empty($str)) {
return false;
}
return mb_strrpos($str, $search);
}
if (!is_int($offset)) {
trigger_error('utf8_strrpos expects parameter 3 to be long', E_USER_WARNING);
return false;
}
$str = mb_substr($str, $offset);
if (($pos = mb_strrpos($str, $search)) !== false) {
return $pos + $offset;
}
return false;
}
/**
* Wrapper around mb_substr.
*
* Return part of a string given character offset (and optionally length).
*
* @param string $str
* @param integer $offset number of UTF-8 characters offset (from left)
* @param integer|false $length (optional) length in UTF-8 characters from offset
*
* @return mixed string or false if failure
*/
function sub($str, $offset, $length = false)
{
if ($length === false) {
return mb_substr($str, $offset);
}
return mb_substr($str, $offset, $length);
}
/**
* Wrapper around mb_strtolower.
*
* Make a string lowercase.
*
* The concept of a characters "case" only exists is some alphabets such as
* Latin, Greek, Cyrillic, Armenian and archaic Georgian - it does not exist in
* the Chinese alphabet, for example. See Unicode Standard Annex #21: Case Mappings.
*
* @param string $str
*
* @return mixed either string in lowercase or false is UTF-8 invalid
*/
function to_lower($str)
{
return mb_strtolower($str);
}
/**
* Wrapper around mb_strtoupper.
*
* Make a string uppercase.
*
* The concept of a characters "case" only exists is some alphabets such as
* Latin, Greek, Cyrillic, Armenian and archaic Georgian - it does not exist in
* the Chinese alphabet, for example. See Unicode Standard Annex #21: Case Mappings
*
* @param string
*
* @return mixed either string in lowercase or false is UTF-8 invalid
*/
function to_upper($str)
{
return mb_strtoupper($str);
}
/**
* UTF-8 aware alternative to ucwords.
*
* Uppercase the first character of each word in a string using mb_convert_case.
*
* @see http://php.net/manual/en/function.ucwords.php
* @see http://php.net/manual/en/function.mb-convert-case.php
* @uses utf8_substr_replace
* @uses utf8_strtoupper
*
* @param string
*
* @return string with first char of each word uppercase
*/
function ucwords($str)
{
return mb_convert_case($str, MB_CASE_TITLE, 'UTF-8');
}