Skip to content

Commit 0bb8d1a

Browse files
authored
mb_*_numericentity Fix a code example (#4708)
* Update mb-decode-numericentity.xml CS * Update mb-encode-numericentity.xml Fix a code example * mb-encode-numericentity.xml Ade `role` attr to highlighting * mb-decode-numericentity.xml Remove the newlines * mb-encode-numericentity.xml Remove the newlines
1 parent ec891f5 commit 0bb8d1a

2 files changed

Lines changed: 72 additions & 59 deletions

File tree

‎reference/mbstring/functions/mb-decode-numericentity.xml‎

Lines changed: 62 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,13 @@
100100
<programlisting role="php">
101101
<![CDATA[
102102
<?php
103-
$convmap = array (
104-
int start_code1, int end_code1, int offset1, int mask1,
105-
int start_code2, int end_code2, int offset2, int mask2,
106-
// ........
107-
int start_codeN, int end_codeN, int offsetN, int maskN );
103+
104+
$convmap = array(
105+
int start_code1, int end_code1, int offset1, int mask1,
106+
int start_code2, int end_code2, int offset2, int mask2,
107+
// ........
108+
int start_codeN, int end_codeN, int offsetN, int maskN
109+
);
108110
// Specify Unicode value for start_codeN and end_codeN
109111
// Add offsetN to value and take bit-wise 'AND' with maskN,
110112
// then convert value to numeric string reference.
@@ -120,64 +122,72 @@ $convmap = array (
120122
<programlisting role="php">
121123
<![CDATA[
122124
<?php
123-
function escape_javascript_string($str) {
124-
$map = [
125-
1,1,1,1,1,1,1,1,1,1,
126-
1,1,1,1,1,1,1,1,1,1,
127-
1,1,1,1,1,1,1,1,1,1,
128-
1,1,1,1,1,1,1,1,1,1,
129-
1,1,1,1,1,1,1,1,0,0, // 49
130-
0,0,0,0,0,0,0,0,1,1,
131-
1,1,1,1,1,0,0,0,0,0,
132-
0,0,0,0,0,0,0,0,0,0,
133-
0,0,0,0,0,0,0,0,0,0,
134-
0,1,1,1,1,1,1,0,0,0, // 99
135-
0,0,0,0,0,0,0,0,0,0,
136-
0,0,0,0,0,0,0,0,0,0,
137-
0,0,0,1,1,1,1,1,1,1,
138-
1,1,1,1,1,1,1,1,1,1,
139-
1,1,1,1,1,1,1,1,1,1, // 149
140-
1,1,1,1,1,1,1,1,1,1,
141-
1,1,1,1,1,1,1,1,1,1,
142-
1,1,1,1,1,1,1,1,1,1,
143-
1,1,1,1,1,1,1,1,1,1,
144-
1,1,1,1,1,1,1,1,1,1, // 199
145-
1,1,1,1,1,1,1,1,1,1,
146-
1,1,1,1,1,1,1,1,1,1,
147-
1,1,1,1,1,1,1,1,1,1,
148-
1,1,1,1,1,1,1,1,1,1,
149-
1,1,1,1,1,1,1,1,1,1, // 249
150-
1,1,1,1,1,1,1, // 255
151-
];
152-
// Char encoding is UTF-8
153-
$mblen = mb_strlen($str, 'UTF-8');
154-
$utf32 = bin2hex(mb_convert_encoding($str, 'UTF-32', 'UTF-8'));
155-
for ($i=0, $encoded=''; $i < $mblen; $i++) {
156-
$u = substr($utf32, $i*8, 8);
157-
$v = base_convert($u, 16, 10);
158-
if ($v < 256 && $map[$v]) {
159-
$encoded .= '\\x'.substr($u, 6,2);
160-
} else if ($v == 2028) {
161-
$encoded .= '\\u2028';
162-
} else if ($v == 2029) {
163-
$encoded .= '\\u2029';
164-
} else {
165-
$encoded .= mb_convert_encoding(hex2bin($u), 'UTF-8', 'UTF-32');
166-
}
167-
}
168-
return $encoded;
125+
126+
function escape_javascript_string($str)
127+
{
128+
$map = [
129+
1,1,1,1,1,1,1,1,1,1,
130+
1,1,1,1,1,1,1,1,1,1,
131+
1,1,1,1,1,1,1,1,1,1,
132+
1,1,1,1,1,1,1,1,1,1,
133+
1,1,1,1,1,1,1,1,0,0, // 49
134+
0,0,0,0,0,0,0,0,1,1,
135+
1,1,1,1,1,0,0,0,0,0,
136+
0,0,0,0,0,0,0,0,0,0,
137+
0,0,0,0,0,0,0,0,0,0,
138+
0,1,1,1,1,1,1,0,0,0, // 99
139+
0,0,0,0,0,0,0,0,0,0,
140+
0,0,0,0,0,0,0,0,0,0,
141+
0,0,0,1,1,1,1,1,1,1,
142+
1,1,1,1,1,1,1,1,1,1,
143+
1,1,1,1,1,1,1,1,1,1, // 149
144+
1,1,1,1,1,1,1,1,1,1,
145+
1,1,1,1,1,1,1,1,1,1,
146+
1,1,1,1,1,1,1,1,1,1,
147+
1,1,1,1,1,1,1,1,1,1,
148+
1,1,1,1,1,1,1,1,1,1, // 199
149+
1,1,1,1,1,1,1,1,1,1,
150+
1,1,1,1,1,1,1,1,1,1,
151+
1,1,1,1,1,1,1,1,1,1,
152+
1,1,1,1,1,1,1,1,1,1,
153+
1,1,1,1,1,1,1,1,1,1, // 249
154+
1,1,1,1,1,1,1, // 255
155+
];
156+
157+
// Char encoding is UTF-8
158+
$mblen = mb_strlen($str, 'UTF-8');
159+
$utf32 = bin2hex(mb_convert_encoding($str, 'UTF-32', 'UTF-8'));
160+
161+
for ($i=0, $encoded=''; $i < $mblen; $i++) {
162+
$u = substr($utf32, $i * 8, 8);
163+
$v = base_convert($u, 16, 10);
164+
165+
if ($v < 256 && $map[$v]) {
166+
$encoded .= '\\x' . substr($u, 6,2);
167+
} else if ($v == 2028) {
168+
$encoded .= '\\u2028';
169+
} else if ($v == 2029) {
170+
$encoded .= '\\u2029';
171+
} else {
172+
$encoded .= mb_convert_encoding(hex2bin($u), 'UTF-8', 'UTF-32');
173+
}
174+
}
175+
176+
return $encoded;
169177
}
170178
171179
// Test data
172180
$convmap = [ 0x0, 0xffff, 0, 0xffff ];
173181
$msg = '';
182+
174183
for ($i=0; $i < 1000; $i++) {
175184
// chr() cannot generate correct UTF-8 data larger value than 128, use mb_decode_numericentity().
176-
$msg .= mb_decode_numericentity('&#'.$i.';', $convmap, 'UTF-8');
185+
$msg .= mb_decode_numericentity('&#' . $i . ';', $convmap, 'UTF-8');
177186
}
178187
179188
// var_dump($msg);
180189
var_dump(escape_javascript_string($msg));
190+
?>
181191
]]>
182192
</programlisting>
183193
</example>

‎reference/mbstring/functions/mb-encode-numericentity.xml‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,16 @@
111111
<programlisting role="php">
112112
<![CDATA[
113113
<?php
114-
$convmap = array (
115-
int start_code1, int end_code1, int offset1, int mask1,
116-
int start_code2, int end_code2, int offset2, int mask2,
117-
........
118-
int start_codeN, int end_codeN, int offsetN, int maskN );
114+
115+
$convmap = array(
116+
int start_code1, int end_code1, int offset1, int mask1,
117+
int start_code2, int end_code2, int offset2, int mask2,
118+
// ........
119+
int start_codeN, int end_codeN, int offsetN, int maskN
120+
);
119121
// Specify Unicode value for start_codeN and end_codeN
120-
// Add offsetN to value and take bit-wise 'AND' with maskN, then
121-
// it converts value to numeric string reference.
122+
// Add offsetN to value and take bit-wise 'AND' with maskN,
123+
// then converts value to numeric string reference.
122124
?>
123125
]]>
124126
</programlisting>
@@ -143,6 +145,7 @@ $convmap = [
143145
0x80, 0x7FF, 0, 0x10FFFF,
144146
0x10000, 0x1FFFFF, 0, 0x10FFFF,
145147
];
148+
146149
var_dump(mb_encode_numericentity($str, $convmap, "utf8"));
147150
?>
148151
]]>

0 commit comments

Comments
 (0)