views:

390

answers:

1

Hi all,

I am wondering if someone could please help me convert a piece of PHP code that is now deprecated.

Here is the single line I am trying to convert:

if(eregi(trim ($request_url_handler[$x]),$this->sys_request_url) && $this->id_found == 0){

It is part of a function that return the configuration settings for a website. Below is the whole function.

// GET CORRECT CONFIG FROM DATABASE
function get_config($db)
{
    global $tbl_prefix;
    $db->query("SELECT cid,urls FROM ".$tbl_prefix."sys_config ORDER BY cid");

    while($db->next_record()){
        $request_url_handler = explode("\n",$db->f("urls"));
        if(empty($request_url_handler[0])) {
            $request_url_handler[0] = "@";
            $this->id_found = 2;
        }

        for($x=0; $x<count($request_url_handler); $x++) {
            if(empty($request_url_handler[$x])) {
                $request_url_handler[$x] = "@";
            }
            if(eregi(trim($request_url_handler[$x]),$this->sys_request_url) && $this->id_found == 0) {
                $this->set_config($db,$db->f("cid"));
                $this->id_found = 1;
            }
        }

        if($this->id_found == 1) {
            return($this->sys_config_vars);
        }
    }

    $this->set_config($db,"");
    return($this->sys_config_vars);
}

Any help would be great ly appreciated. I only found the the eregi function was deprecated since I updated XAMPP to 1.7.3.

+3  A: 

Try replacing:

if(eregi(trim($request_url_handler[$x]),$this->sys_request_url) && $this->id_found == 0) {

with:

$request_url_handler[$x] = trim($request_url_handler[$x]);
if( preg_match("/$request_url_handler[$x]/i",$this->sys_request_url) && $this->id_found == 0) {

eregi is deprecated and we need to use preg_match with the i option as its replacement.

In general

eregi($regex,$input)

can be replaced with:

preg_match("/$regex/i",$input)

EDIT:

Its assumed that $regex above does not contain any / which is used as delimiters. If it does then you'll have to use a different delimiter say @ or # or | not contained in $regex

preg_match("#$regex#i",$input)

Alternatively you can also escape all the occurrences of the delimiter in the $regex.

codaddict
Thanks a million for helping me out on this matter. It comes up with an error: Warning: preg_match() [function.preg-match]: Unknown modifier '/' in C:\xampp\htdocs\VirtualCMS\system\class_config.php on line 57
Jason
You will need to replace '/' with '\/' in case the regex contains a slash.
too much php
@Jason: I've updated my answer. Try using a different delimiter in preg_match.
codaddict
Aah thanks unicornaddict, changing the delimeter worked a treat, sorry for the trouble, thanks for the help and knowledge.
Jason