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
|
<?php
// if mb functions are available, set internal encoding to UTF8
if (is_callable('mb_internal_encoding')) {
mb_internal_encoding("UTF-8");
ini_set("mbstring.internal_encoding", 'UTF-8');
}
/**
* Parses a string using mb_parse_str() if available.
* NOTE: This differs from parse_str() by returning the results
* instead of placing them in the local scope!
*
* @param str $str
* @return array
*/
function elgg_parse_str($str) {
if (is_callable('mb_parse_str')) {
mb_parse_str($str, $results);
} else {
parse_str($str, $results);
}
return $results;
}
// map string functions to their mb_str_func alternatives
// and wrap them in elgg_str_fun()
// list of non-mb safe string functions to wrap in elgg_*()
// only will work with mb_* functions that take the same
// params in the same order as their non-mb safe counterparts.
$str_funcs = array(
// can't wrap parse_str() because of its 2nd parameter.
//'parse_str',
'split',
'stristr',
'strlen',
'strpos',
'strrchr',
'strripos',
'strrpos',
'strstr',
'strtolower',
'strtoupper',
'substr_count',
'substr'
);
$eval_statement = '';
foreach ($str_funcs as $func) {
// create wrapper function passing in the same args as given
$mb_func = "mb_$func";
$eval_statement .= "
function elgg_$func() {
\$args = func_get_args();
if (is_callable('$mb_func')) {
return call_user_func_array('$mb_func', \$args);
}
return call_user_func_array('$func', \$args);
}
";
}
eval($eval_statement);
// TODO: Other wrapper functions
|