php 测试ereg_replace()替换eregi_replace()ereg()eregi()split()(不推荐使用php的东西)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 测试ereg_replace()替换eregi_replace()ereg()eregi()split()(不推荐使用php的东西)相关的知识,希望对你有一定的参考价值。

<?php
/**
 * php 5.3 and utf-8 "compatibility layer"
 *
 * @author mnt
 */

if (!function_exists('force_utf8')) {
    function force_utf8($str)
    {
        if (mb_detect_encoding($str, 'UTF-8', true) === false) {
            return iconv('ISO-8859-1', 'UTF-8', $str);
        }
        return $str;
    }
}

if (!isset($HTTP_GET_VARS)) {
    $GLOBALS['HTTP_GET_VARS'] = $_GET;
    $GLOBALS['HTTP_POST_VARS'] = $_POST;
    $GLOBALS['HTTP_POST_FILES'] = $_FILES;
    $GLOBALS['HTTP_SESSION_VARS'] = $_SESSION;
    $GLOBALS['HTTP_RAW_POST_DATA'] = file_get_contents("php://input");
    $GLOBALS['HTTP_SERVER_VARS'] = $_SERVER;
}

if (!function_exists('call_user_method_array')) {
    function call_user_method_array($method, $obj, $arr)
    {
        return call_user_func_array(array($obj, $method), $arr);
    }
}

if (!function_exists('call_user_method')) {
    function call_user_method()
    {
        $arr = func_get_args();
        $method = array_shift($arr);
        $obj = array_shift($arr);

        return call_user_func_array(array($obj, $method), $arr);
    }
}

if (!function_exists('split')) {
    function split($pattern, $subject, $limit = -1)
    {
        $pattern = addcslashes($pattern, '/');
        return preg_split("/$pattern/", $subject, $limit);
    }
}

if (!function_exists('spliti')) {
    function spliti($pattern, $subject, $limit = -1)
    {
        $pattern = addcslashes($pattern, '/');
        return preg_split("/$pattern/i", $subject, $limit);
    }
}

if (!function_exists('ereg_replace')) {
    function ereg_replace($pattern, $repl, $subj)
    {
        $hasStartEnd = strpos($pattern, "^") !== false || strpos($pattern, "$") !== false;
        $isRegEx = $hasStartEnd || strpos($pattern, "[") !== false || strpos($pattern, "(") !== false;

        if ($isRegEx) {
            $pattern = addcslashes($pattern, '/');
            return preg_replace("/$pattern/", $repl, $subj);
        }

        return str_replace($pattern, $repl, $subj);
    }
}


if (!function_exists('eregi_replace')) {
    function eregi_replace($pattern, $repl, $subj)
    {
        $hasStartEnd = strpos($pattern, "^") !== false || strpos($pattern, "$") !== false;
        $isRegEx = $hasStartEnd || strpos($pattern, "[") !== false || strpos($pattern, "(") !== false;

        if ($isRegEx) {
            $pattern = addcslashes($pattern, '/');
            return preg_replace("/$pattern/i", $repl, $subj);
        }

        return str_replace($pattern, $repl, $subj);
    }
}

if (!function_exists('ereg')) {
    function ereg($pattern, $subject, &$matches = array())
    {
        $pattern = str_replace('/', '\/', $pattern);
        $result = preg_match("/$pattern/", $subject, $matches);
        if (is_array($matches) && count($matches) == 0) {
            $matches = null;
        }
        return $result;
    }
}

if (!function_exists('eregi')) {
    function eregi($pattern, $subject, &$matches = array())
    {
        $pattern = str_replace('/', '\/', $pattern);
        $result = preg_match("/$pattern/i", $subject, $matches);
        if (is_array($matches) && count($matches) == 0) {
            $matches = null;
        }
        return $result;
    }
}

以上是关于php 测试ereg_replace()替换eregi_replace()ereg()eregi()split()(不推荐使用php的东西)的主要内容,如果未能解决你的问题,请参考以下文章

关于php报错:Deprecated: Function ereg_replace() 的解决方法

php base64加密图替换

PHPereg_repalce( )

php str_replace()——字符串替换函数

php 版本升高后 会出现 之Deprecated: Function ereg_replace() is deprecated的解决方法

PHP 清除HTML代码空格回车换行符的函数