Skip to content

Instantly share code, notes, and snippets.

@emente
Last active May 24, 2019 07:03
Show Gist options
  • Save emente/8639b8370a0264b08e0a98b19dfb599d to your computer and use it in GitHub Desktop.
Save emente/8639b8370a0264b08e0a98b19dfb599d to your computer and use it in GitHub Desktop.
tested replacements for ereg_replace() eregi_replace() ereg() eregi() split() (deprecated php stuff)
<?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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment