diff --git a/lib/parser.php b/lib/parser.php index a4fd1164c9378..564a715e4d27e 100644 --- a/lib/parser.php +++ b/lib/parser.php @@ -1444,7 +1444,17 @@ public function parse($input) { if ( ! function_exists( 'ucs2length' ) ) { function ucs2length( $string ) { - return (int) strlen( iconv( 'UTF-8', 'UTF-16le', $string ) ) / 2; + if ( function_exists( 'iconv' ) ) { + return (int) strlen( iconv( 'UTF-8', 'UTF-16LE', $string ) ) / 2; + } + + if ( function_exists( 'iconv_fallback' ) ) { + return (int) strlen( iconv_fallback( 'UTF-8', 'UTF16-LE', $string ) ); + } + + // what should we do here? + // this is wrong. + return (int) strlen( $string ); } } diff --git a/packages/block-serialization-default-parser/parser.php b/packages/block-serialization-default-parser/parser.php index 2e61b178b1c8e..3ec1fa16f81c3 100644 --- a/packages/block-serialization-default-parser/parser.php +++ b/packages/block-serialization-default-parser/parser.php @@ -493,6 +493,16 @@ function add_block_from_stack( $end_offset = null ) { } static function ucs2length( $string ) { - return (int) strlen( iconv( 'UTF-8', 'UTF-16le', $string ) ) / 2; + if ( function_exists( 'iconv' ) ) { + return (int) strlen( iconv( 'UTF-8', 'UTF-16LE', $string ) ) / 2; + } + + if ( function_exists( 'iconv_fallback' ) ) { + return (int) strlen( iconv_fallback( 'UTF-8', 'UTF16-LE', $string ) ); + } + + // what should we do here? + // this is wrong. + return (int) strlen( $string ); } } diff --git a/packages/block-serialization-spec-parser/grammar.pegjs b/packages/block-serialization-spec-parser/grammar.pegjs index cd75dab727dbd..2796b13e8d043 100644 --- a/packages/block-serialization-spec-parser/grammar.pegjs +++ b/packages/block-serialization-spec-parser/grammar.pegjs @@ -52,7 +52,17 @@ if ( ! function_exists( 'ucs2length' ) ) { function ucs2length( $string ) { - return (int) strlen( iconv( 'UTF-8', 'UTF-16le', $string ) ) / 2; + if ( function_exists( 'iconv' ) ) { + return (int) strlen( iconv( 'UTF-8', 'UTF-16LE', $string ) ) / 2; + } + + if ( function_exists( 'iconv_fallback' ) ) { + return (int) strlen( iconv_fallback( 'UTF-8', 'UTF16-LE', $string ) ); + } + + // what should we do here? + // this is wrong. + return (int) strlen( $string ); } } diff --git a/packages/block-serialization-spec-parser/index.js b/packages/block-serialization-spec-parser/index.js index 421877a23f805..05aa1c01c520b 100644 --- a/packages/block-serialization-spec-parser/index.js +++ b/packages/block-serialization-spec-parser/index.js @@ -1484,7 +1484,17 @@ if ( ! function_exists( 'ucs2length' ) ) { function ucs2length( $string ) { - return (int) strlen( iconv( 'UTF-8', 'UTF-16le', $string ) ) / 2; + if ( function_exists( 'iconv' ) ) { + return (int) strlen( iconv( 'UTF-8', 'UTF-16LE', $string ) ) / 2; + } + + if ( function_exists( 'iconv_fallback' ) ) { + return (int) strlen( iconv_fallback( 'UTF-8', 'UTF16-LE', $string ) ); + } + + // what should we do here? + // this is wrong. + return (int) strlen( $string ); } }