eregi() deprecated - I am not sure how to rewrite this code [duplicate] - php

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
How to validate an Email in PHP?
(7 answers)
How to validate an email address in PHP
(15 answers)
Closed 9 years ago.
Deprecated code:
function validate_email($email)
{
return eregi("^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$", $email);
}
I am a JavaScript beginner. The above code gives an error. I am not too sure how to rewrite using preg_match.

Try this instead
function validate_email($email)
{
return (1 === preg_match("/^[_\.0-9a-zA-Z-]+#([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email));
}
preg_match() should be used rather than eregi. You must include the slashes at the begining and end of the pattern. And finally, the "/i" at the end of the pattern makes it case-insensitive.

Related

What is the difference between in_array() and #in_array() in php? [duplicate]

This question already has answers here:
# symbol before php function [duplicate]
(3 answers)
What is the use of the # symbol in PHP?
(11 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 4 years ago.
can you please help i just want to know why we use #in_array function.
$name = array("ravi", "ram", "rani", 87);
if (in_array("ravi", $name, TRUE)){
}
if (#in_array("ravi", $name, TRUE)){
}
The "#" symbol turn off the errors. It is not a good idea to use it.
PHP Error Control Operators
PHP supports one error control operator:
the at sign (#
http://php.net/manual/en/language.operators.errorcontrol.php

PHP 5.1 to PHP 7.1 broke contact form [duplicate]

This question already has answers here:
ereg/eregi replacement for PHP 5.3 [duplicate]
(4 answers)
How to replace eregi()
(2 answers)
How can I convert ereg expressions to preg in PHP?
(4 answers)
Alternative to eregi() in php [duplicate]
(1 answer)
How to change PHP's eregi to preg_match [duplicate]
(2 answers)
Closed 4 years ago.
The web hosting service provider upgraded to PHP 7.1 and it broke the contact form of the page. I've narrowed it down to this piece of code:
function check_email($mail)
{
$email_host = explode("#", $mail);
$email_host = $email_host['1'];
$email_resolved = gethostbyname($email_host);
if ($email_resolved != $email_host && #eregi("^[0-9a-z]([-_.~]?[0-9a-z])*#[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$mail))
$valid = 1; return $valid;
}
I've found that the eregi function is no longer supported in PHP 7.1 but I dont know how and with what i should replace it.
Have a look at the documentation of eregi function in php.net:
Warning
This function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.
Alternatives to this function include:
preg_match() (with the i (PCRE_CASELESS) modifier)
You should always have a look there when it comes do deprecated functions.
For validating email addresses you could also use filter_var now:
if (filter_var('test#example.com', FILTER_VALIDATE_EMAIL)) {
echo "Email valid.";
}

What to replace eregi() with? [duplicate]

This question already has answers here:
ereg/eregi replacement for PHP 5.3 [duplicate]
(4 answers)
Closed 5 years ago.
What is eregi() replaced with in this instance?
// get value of text inbetween tags
function getContentByTag($tag1, $tag2, $string)
{
if (eregi("$tag1(.*)$tag2", $string, $out)) {
$outdata = $out[1];
}
return $outdata;
}
This post is not a duplicate post as the 3rd example within the referenced post the answer is for that specific usage. I'm guessing my usage is different as the referenced post answers are not working for me.
Since PHP 7 you must replace it with
preg_match("/$tag1(.*)$tag2/i", $string, $out)

Eregi_replace to preg_replace conversion needed [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 3 years ago.
i need to convert this in order to use preg_replace because eregi_replace is deprecated
$n=eregi_replace(")+.+","",$value);
$id=eregi_replace(".+)","<a hсref='http://www.company.com/product.php?id=".$n."'>",$value);
$newstr.="".$id."</a><br>";
//..... must be converted
its very unclear to me how that would look in preg_replace
$gexpieces = explode(")", $value);
$n=$gexpieces[0];//n is product id
unset($gexpieces[0]);
$prodname = implode($gexpieces);
$newstr.="<a href='http://www.company.com/product.php?id=".$n."'>".$prodname."</a><br/><br/>";
nevermind, done it myself.

Alternative to eregi() in php [duplicate]

This question already has answers here:
How can I convert ereg expressions to preg in PHP?
(4 answers)
Closed 10 years ago.
So, i was using eregi in my mail script, but as of lately, i get the error that the function is deprecated.
So, what is the easiest way to replace the following bit of code:
if (!eregi("^[A-Z0-9._%-]+#[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email'])))?
Any help is appreciated :)
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';

Categories