Solved

How to rewrite ereg to preg_match

Posted on 2013-01-01
3
400 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 109

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 109

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

770 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