Solved

Function eregi() is deprecated

Posted on 2014-02-22
2
306 Views
Last Modified: 2014-02-23
I believe preg_match should be used. What will be the code? Thank you.


1) if(ereg("^[A-Za-z0-9_]{1,255}$",$this->username))

2) if(eregi("^[https?://.+\..+]{1,255}",$this->url))

3) if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$",$this->email))

4) if(ereg("^[0-9]{1,6}$",$this->hits))

5)   if(eregi("^[https?://.+\..+]{1,255}",$this->url)
&& eregi("[.+\@.+\..+]{1,255}",$this->email)

Open in new window

0
Comment
Question by:seopti
  • 2
2 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39879977
Usually this is as simple as changing to preg_match() by adding a regex delimiter and the appropriate flags.  Example:

"^[https?://.+\..+]{1,255}" for eregi() would become
"#^[https?://.+\..+]{1,255}#i" for preg_match()

Another example...
if(ereg("^[0-9]{1,6}$",$this->hits)) would become something like this:
"/^[0-9]{1,6}$/"

You may fine it easier to make the conversion if you write out the regular expressions in a way that self-explains the expression.  Example:
$rgx 
= '/'        // REGEX DELIMITER
. '^'        // AT START OF THE STRING
. '[0-9]'    // CHARACTER CLASS OF DIGITS ONLY
. '{1,6}'    // WITH LENGTH OF 1-6 CHARACTERS
. '$'        // AT END OF STRING
. '/'        // REGEX DELIMITER
;

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39879985
Here's another example.  Original ereg() expression:
"^[A-Za-z0-9_]{1,255}$"

Open in new window

Updated for preg-type expressions:
$rgx
= '#'       // REGEX DELIMITER
. '^'       // AT START OF STRING
. '['       // START CHARACTER CLSS
. 'A-Z'     // UPPER CASE ALPHA
. 'a-z'     // LOWER CASE ALPHA
. '0-9'     // NUMERIC DIGITS
. '_'       // THE UNDERSCORE
. ']'       // END OF CHARACTER CLASS
. '{1,255}' // LENGTH FROM 1 TO 255
. '$'       // AT END OF STRING
. '#'       // REGEX DELIMITER
;

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question