File tree Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Expand file tree Collapse file tree 2 files changed +39
-2
lines changed Original file line number Diff line number Diff line change @@ -81,6 +81,39 @@ use Ddeboer\Transcoder\Transcoder;
81
81
$transcoder->transcode('España', null, 'UTF-8');
82
82
```
83
83
84
+ ### Error handling
85
+
86
+ PHP’s ` mv_convert_encoding ` and ` iconv ` are inconvenient to use because they
87
+ generate notices and warnings instead of proper exceptions. This library fixes
88
+ that:
89
+
90
+
91
+ ``` php
92
+ use Ddeboer\Transcoder\Exception\UndetectableEncodingException;
93
+ use Ddeboer\Transcoder\Exception\UnsupportedEncodingException;
94
+ use Ddeboer\Transcoder\Exception\IllegalCharacterException;
95
+
96
+ $input = 'España';
97
+
98
+ try {
99
+ $transcoder->transcode($input);
100
+ } catch (UndetectableEncodingException $e) {
101
+ // Failed to automatically detect $input’s encoding
102
+ }
103
+
104
+ try {
105
+ $transcoder->transcode($input, null, 'not-a-real-encoding');
106
+ } catch (UnsupportedEncodingException $e) {
107
+ // ‘not-a-real-encoding’ is an unsupported encoding
108
+ }
109
+
110
+ try {
111
+ $transcoder->transcode('Illegal quotes: ‘ ’', null, 'iso-8859-1');
112
+ } catch (IllegalCharacterException $e) {
113
+ // Curly quotes ‘ ’ are illegal in ISO-8859-1
114
+ }
115
+ ```
116
+
84
117
### Transcoder fallback
85
118
86
119
In general, ` mb_convert_encoding ` is faster than ` iconv ` . However, as ` iconv `
Original file line number Diff line number Diff line change @@ -46,7 +46,11 @@ public function testDetectEncoding()
46
46
*/
47
47
public function testUndetectableEncoding ()
48
48
{
49
- $ result = $ this ->transcoder ->transcode ('‘España’ ' , null , 'windows-1252 ' );
49
+ $ result = $ this ->transcoder ->transcode (
50
+ '‘curly quotes make this incompatible with 1252’ ' ,
51
+ null ,
52
+ 'windows-1252 '
53
+ );
50
54
$ this ->transcoder ->transcode ($ result );
51
55
}
52
56
@@ -63,7 +67,7 @@ public function getStrings()
63
67
{
64
68
return [
65
69
['‘España’ ' , 'windows-1252 ' ],
66
- ['España ' , 'iso-8859-1 ' ]
70
+ ['‘ España ' , 'iso-8859-1 ' ]
67
71
];
68
72
}
69
73
}
You can’t perform that action at this time.
0 commit comments