Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to rewrite ereg to preg_match

Posted on 2013-01-01
3
Medium Priority
?
426 Views
Last Modified: 2013-01-01
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
Comment
Question by:tjyoung
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 total points
ID: 38734452
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
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 38734568
Thanks that was it!
Have a great 2013 Ray.
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 38734570
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

719 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