|
1
|
|
|
<?php |
|
2
|
|
|
/* code by lars(at)ioflux.net, reproduced from www.php.net */ |
|
3
|
|
|
/* todo: add a list of common unicode characters (&euro, etc.) */ |
|
4
|
|
|
|
|
5
|
|
|
class unicode { |
|
6
|
|
|
|
|
7
|
|
|
function utf8convert($string, $maxchar=0x7F, $entities=true) { |
|
8
|
|
|
|
|
9
|
|
|
$returns = ""; |
|
10
|
|
|
$UTF8len = array( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
|
11
|
|
|
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, |
|
12
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, |
|
13
|
|
|
2, 2, 3, 3, 3, 3, 4, 4, 5, 6); |
|
14
|
|
|
$pos = 0; |
|
15
|
|
|
$total = strlen($string); |
|
16
|
|
|
|
|
17
|
|
|
do { |
|
18
|
|
|
$c = ord($string[$pos]); |
|
19
|
|
|
$len = $UTF8len[($c >> 2) & 0x3F]; |
|
20
|
|
|
switch ($len) |
|
21
|
|
|
{ |
|
22
|
|
|
case 6: |
|
23
|
|
|
$u = $c & 0x01; |
|
24
|
|
|
break; |
|
25
|
|
|
case 5: |
|
26
|
|
|
$u = $c & 0x03; |
|
27
|
|
|
break; |
|
28
|
|
|
case 4: |
|
29
|
|
|
$u = $c & 0x07; |
|
30
|
|
|
break; |
|
31
|
|
|
case 3: |
|
32
|
|
|
$u = $c & 0x0F; |
|
33
|
|
|
break; |
|
34
|
|
|
case 2: |
|
35
|
|
|
$u = $c & 0x1F; |
|
36
|
|
|
break; |
|
37
|
|
|
case 1: |
|
38
|
|
|
$u = $c & 0x7F; |
|
39
|
|
|
break; |
|
40
|
|
|
case 0: /* unexpected start of a new character */ |
|
41
|
|
|
$u = $c & 0x3F; |
|
42
|
|
|
$len = 5; |
|
43
|
|
|
break; |
|
44
|
|
|
} |
|
45
|
|
|
while (--$len && (++$pos < $total && $c = ord($string[$pos]))) { |
|
46
|
|
|
if (($c & 0xC0) == 0x80) { |
|
47
|
|
|
$u = ($u << 6) | ($c & 0x3F); |
|
|
|
|
|
|
48
|
|
|
} else { |
|
49
|
|
|
/* unexpected start of a new character */ |
|
50
|
|
|
$pos--; |
|
51
|
|
|
break; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
if ($u <= $maxchar) { |
|
55
|
|
|
$returns .= chr($u); |
|
56
|
|
|
} else if ($entities) { |
|
57
|
|
|
$returns .= '&#'.$u.';'; |
|
58
|
|
|
} else { |
|
59
|
|
|
$returns .= '?'; |
|
60
|
|
|
} |
|
61
|
|
|
} while (++$pos < $total); |
|
62
|
|
|
return $returns; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
function utf8toiso8859($string, $entities=true) { |
|
66
|
|
|
return unicode::utf8convert($string, 0xFF, $entities); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
function convertToUTF8($charset, $string) { |
|
71
|
|
|
global $charset_table; |
|
72
|
|
|
if (!$charset_table[$charset]) { |
|
73
|
|
|
$tablename = preg_replace('/[^a-z0-9_-]*/', '', strtolower($charset)); |
|
74
|
|
|
include("mod_unicode.$tablename.php"); |
|
75
|
|
|
$charset_table[$charset] = $table; |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
if (is_string($string)) { |
|
78
|
|
|
$result = ""; |
|
79
|
|
|
$strlen = strlen($string); |
|
80
|
|
|
for ($i=0; $i<$strlen; $i++) { |
|
81
|
|
|
$result .= $charset_table[$charset][ord($string[$i])]; |
|
82
|
|
|
} |
|
83
|
|
|
} else { |
|
84
|
|
|
$result = $string; |
|
85
|
|
|
} |
|
86
|
|
|
return $result; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
class pinp_unicode extends unicode { |
|
91
|
|
|
|
|
92
|
|
|
function _utf8convert($string, $maxchar=0x7F, $entities=true) { |
|
93
|
|
|
return pinp_unicode::utf8convert($string, $maxchar, $entities); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
function _utf8toiso8859($string, $entities=true) { |
|
97
|
|
|
return pinp_unicode::utf8toiso8859($string, $entities); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
function _convertToUTF8($charset, $string) { |
|
101
|
|
|
return pinp_unicode::convertToUTF8($charset, $string); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: