Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 435
  • Last Modified:

How to rewrite ereg to preg_match

Hi,
I'm trying to figure out how to change the below code so it doesn't use ereg as I'm getting a deprecated error and I believe I should be using preg_match. I simply dropped in preg_match and not surprisingly, it failed with this error:
Warning: preg_match(): No ending delimiter '^'

What I'm trying to accomplish it to tell if the '$fromcaller' variable is an ip number or not and if so, 'do this' or else 'do that'. If there is a better/cleaner way of doing it, I'm all ears.

This is the snippet I'm using:
if (ereg("^([0-9]{1,3})x2E([0-9]{1,3})x2E([0-9]{1,3})x2E([0-9]{1,3})$", $fromcaller, $digit)) {
if (($digit[1] <= 255) && ($digit[2] <= 255) && ($digit[3] <= 255) && ($digit[4] <= 255)) {

Open in new window

0
tjyoung
Asked:
tjyoung
  • 2
1 Solution
 
Ray PaseurCommented:
Preg expressions need a "regex delimiter" and it's often as simple as adding the delimiter.

$rgx 
= '#'  // DELIMITER
. "^([0-9]{1,3})x2E([0-9]{1,3})x2E([0-9]{1,3})x2E([0-9]{1,3})$"
. '#' // DELIMITER
;

Open in new window

Try this and see... ~Ray
0
 
tjyoungAuthor Commented:
Thanks that was it!
Have a great 2013 Ray.
0
 
Ray PaseurCommented:
Thanks for the points, and a great New Year to you, too!

If you find that you have eregi() in your code, that is the case-insensitive version of the regular expression.  In that case, you can add the letter i after the last regex delimiter and the preg engine will become case-insensitive.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now