Solved

How to rewrite ereg to preg_match

Posted on 2013-01-01
3
388 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
  • 2
3 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 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 108

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now