对mbstring.func_overload不易碎的substr()实现

原型

RandomCompat_substr( string $binary_string, int $start, int $length = null )

描述

这个版本只使用默认的substr()

参数

$binary_string (string) (Required)

$start (int) (Required)

$length (int) (Optional) (可选的)

返回值

(string)  

源文件

路径:wp-includes/random_compat/byte_safe_strings.php

<?php
...
        function RandomCompat_substr($binary_string, $start, $length = null)
        {
            if (!is_string($binary_string)) {
                throw new TypeError(
                    'RandomCompat_substr(): First argument should be a string'
                );
            }
            if (!is_int($start)) {
                throw new TypeError(
                    'RandomCompat_substr(): Second argument should be an integer'
                );
            }
            if ($length !== null) {
                if (!is_int($length)) {
                    throw new TypeError(
                        'RandomCompat_substr(): Third argument should be an integer, or omitted'
                    );
                }
                return substr($binary_string, $start, $length);
            }
            return substr($binary_string, $start);
        }
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/randomcompat_substr/