[doc-en] master: mb_*_numericentity Fix a code example (#4708)

From: Date: Thu, 03 Sep 2026 09:16:55 +0000
Subject: [doc-en] master: mb_*_numericentity Fix a code example (#4708)
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-23707@lists.php.net to get a copy of this message
Author: Mikhail Alferov (mmalferov) Committer: GitHub (web-flow) Pusher: lacatoire Date: 2026-09-03T11:16:52+02:00 Commit: https://github.com/php/doc-en/commit/0bb8d1a0dda028c1086f07dbae25d320850d7a90 Raw diff: https://github.com/php/doc-en/commit/0bb8d1a0dda028c1086f07dbae25d320850d7a90.diff 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 Changed paths: M reference/mbstring/functions/mb-decode-numericentity.xml M reference/mbstring/functions/mb-encode-numericentity.xml Diff: diff --git a/reference/mbstring/functions/mb-decode-numericentity.xml b/reference/mbstring/functions/mb-decode-numericentity.xml index af2002729281..39b4af220bfb 100644 --- a/reference/mbstring/functions/mb-decode-numericentity.xml +++ b/reference/mbstring/functions/mb-decode-numericentity.xml @@ -100,11 +100,13 @@ <programlisting role="php"> <![CDATA[ <?php -$convmap = array ( - int start_code1, int end_code1, int offset1, int mask1, - int start_code2, int end_code2, int offset2, int mask2, - // ........ - int start_codeN, int end_codeN, int offsetN, int maskN ); + +$convmap = array( + int start_code1, int end_code1, int offset1, int mask1, + int start_code2, int end_code2, int offset2, int mask2, + // ........ + int start_codeN, int end_codeN, int offsetN, int maskN +); // Specify Unicode value for start_codeN and end_codeN // Add offsetN to value and take bit-wise 'AND' with maskN, // then convert value to numeric string reference. @@ -120,64 +122,72 @@ $convmap = array ( <programlisting role="php"> <![CDATA[ <?php -function escape_javascript_string($str) { - $map = [ - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,0,0, // 49 - 0,0,0,0,0,0,0,0,1,1, - 1,1,1,1,1,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - 0,1,1,1,1,1,1,0,0,0, // 99 - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, // 149 - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, // 199 - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, // 249 - 1,1,1,1,1,1,1, // 255 - ]; - // Char encoding is UTF-8 - $mblen = mb_strlen($str, 'UTF-8'); - $utf32 = bin2hex(mb_convert_encoding($str, 'UTF-32', 'UTF-8')); - for ($i=0, $encoded=''; $i < $mblen; $i++) { - $u = substr($utf32, $i*8, 8); - $v = base_convert($u, 16, 10); - if ($v < 256 && $map[$v]) { - $encoded .= '\\x'.substr($u, 6,2); - } else if ($v == 2028) { - $encoded .= '\\u2028'; - } else if ($v == 2029) { - $encoded .= '\\u2029'; - } else { - $encoded .= mb_convert_encoding(hex2bin($u), 'UTF-8', 'UTF-32'); - } - } - return $encoded; + +function escape_javascript_string($str) +{ + $map = [ + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,0,0, // 49 + 0,0,0,0,0,0,0,0,1,1, + 1,1,1,1,1,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,1,1,1,1,1,1,0,0,0, // 99 + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, // 149 + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, // 199 + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, // 249 + 1,1,1,1,1,1,1, // 255 + ]; + + // Char encoding is UTF-8 + $mblen = mb_strlen($str, 'UTF-8'); + $utf32 = bin2hex(mb_convert_encoding($str, 'UTF-32', 'UTF-8')); + + for ($i=0, $encoded=''; $i < $mblen; $i++) { + $u = substr($utf32, $i * 8, 8); + $v = base_convert($u, 16, 10); + + if ($v < 256 && $map[$v]) { + $encoded .= '\\x' . substr($u, 6,2); + } else if ($v == 2028) { + $encoded .= '\\u2028'; + } else if ($v == 2029) { + $encoded .= '\\u2029'; + } else { + $encoded .= mb_convert_encoding(hex2bin($u), 'UTF-8', 'UTF-32'); + } + } + + return $encoded; } // Test data $convmap = [ 0x0, 0xffff, 0, 0xffff ]; $msg = ''; + for ($i=0; $i < 1000; $i++) { // chr() cannot generate correct UTF-8 data larger value than 128, use mb_decode_numericentity(). - $msg .= mb_decode_numericentity('&#'.$i.';', $convmap, 'UTF-8'); + $msg .= mb_decode_numericentity('&#' . $i . ';', $convmap, 'UTF-8'); } // var_dump($msg); var_dump(escape_javascript_string($msg)); +?> ]]> </programlisting> </example> diff --git a/reference/mbstring/functions/mb-encode-numericentity.xml b/reference/mbstring/functions/mb-encode-numericentity.xml index 4aa19143400f..9ccf590f161a 100644 --- a/reference/mbstring/functions/mb-encode-numericentity.xml +++ b/reference/mbstring/functions/mb-encode-numericentity.xml @@ -111,14 +111,16 @@ <programlisting role="php"> <![CDATA[ <?php -$convmap = array ( - int start_code1, int end_code1, int offset1, int mask1, - int start_code2, int end_code2, int offset2, int mask2, - ........ - int start_codeN, int end_codeN, int offsetN, int maskN ); + +$convmap = array( + int start_code1, int end_code1, int offset1, int mask1, + int start_code2, int end_code2, int offset2, int mask2, + // ........ + int start_codeN, int end_codeN, int offsetN, int maskN +); // Specify Unicode value for start_codeN and end_codeN -// Add offsetN to value and take bit-wise 'AND' with maskN, then -// it converts value to numeric string reference. +// Add offsetN to value and take bit-wise 'AND' with maskN, +// then converts value to numeric string reference. ?> ]]> </programlisting> @@ -143,6 +145,7 @@ $convmap = [ 0x80, 0x7FF, 0, 0x10FFFF, 0x10000, 0x1FFFFF, 0, 0x10FFFF, ]; + var_dump(mb_encode_numericentity($str, $convmap, "utf8")); ?> ]]>

« previous php.doc.cvs (#23707) next »