Alternative for Deprecated PHP Function: Eregi_Replace

Alternative for deprecated PHP function: eregi_replace

preg_replace

https://php.net/preg-replace

$pattern = "/([a-z0-9][_a-z0-9.-]+@([0-9a-z][_0-9a-z-]+\.)+[a-z]{2,6})/i";
$replace = "<a href=\"mailto:\\1\">\\1</a>";
$text = preg_replace($pattern, $replace, $text);

Deprecated: Function eregi() is deprecated in

eregi() is deprecated as of PHP 5.3, use preg_match() instead.

Note that preg_match() is only case insensitive when you pass the i modifier in your regular expression.

include 'db_connect.php'; 
if(isset($_POST['Submit']))
{
$acc_type=ucwords($_POST['acc_type']);
$minbalance=ucwords($_POST['minbalance']);

// Removed A-Z here, since the regular expression is case-insensitive
if (!preg_match("/^[a-z ]+$/i", stripslashes(trim($acc_type))))//line 20
{
echo "Enter Valid Data for Account Type!";
exit(0);
}
else
{
// \d and 0-9 do the same thing
if (!preg_match("/^[\d ]+$/", stripslashes(trim($minbalance))))//line 27
{
}
}
}

Deprecated: Function eregi_replace()

the entire ereg family of functions are deprecated in PHP and will at some point be removed from the language. The replacement is the preg family. For the most part, the change is simple:

preg_replace('/[^<>]>/i', '', $question);
^-- ^ ^^
  1. change ereg to preg
  2. add delimeters (/)
  3. for case insensitive matches (eregi), add the i modifier

Deprecated: Function ereg() is deprecated

You must use preg_match instead of ereg because the last one is deprecated.

Replacing it is not a big deal:

ereg( "[][{}()*+?.\\^$|]", $_REQUEST['name'] )

will become:

preg_match( "/[][{}()*+?.\\^$|]/", $_REQUEST['name'] )

p.s. I had to modify more than one hundred files while I was porting my old project to PHP 5.3 to avoid manually modifying I've used following script to do it for me:

function replaceEregWithPregMatch($path) {
$content = file_get_contents($path);
$content = preg_replace('/ereg\(("|\')(.+)(\"|\'),/',
"preg_match('/$2/',",
$content);
file_put_contents($path, $content);
}

I hope it helps.

Function ereg_replace() is deprecated - How to clear this bug?

Switch to preg_replaceDocs and update the expression to use preg syntax (PCRE) instead of ereg syntax (POSIX) where there are differencesDocs (just as it says to do in the manual for ereg_replaceDocs).

Alternative to eregi() in php

 if (!preg_match("/^[A-Z0-9.%-]+@[A-Z0-9.%-]+.[A-Z]{2,4}$/", trim($_POST['email'])))

Using preg_match.

Because ereg_* functions is deprecated in PHP >= 5.3

Also for email validation better used filter_var

if (!filter_var(trim($_POST['email']), FILTER_VALIDATE_EMAIL))
echo 'Email is incorrect';

Good alternative to eregi() in PHP

stristr achieves exactly the same result as eregi (at least when you don't use regular expressions):

if (!stristr($fileName, '.php'))
$filename.='.php';

You could also make a "fake" eregi this way:

if (!function_exists('eregi')) {
function eregi($find, $str) {
return stristr($str, $find);
}
}

Update: Note that stristr doesn't accept regular expressions as eregi does, and for this specific case (checking the extension), you'd better go with vartec's solution.

Function eregi_replace() is deprecated

When a function is deprecated, it means it's not supported anymore and the use of it is discouraged. In fact, all eregi functions are deprecated.

You should try another function, such as preg_replace(). This could mean you have to edit your regular expression.

This should work

$response = preg_replace ("/\s+/", " ", $response);
$response = preg_replace ("/[\r\n]/", "", $response);


Related Topics



Leave a reply



Submit