Solved

ereg_replace() to preg_replace()

Posted on 2013-11-05
12
274 Views
Last Modified: 2013-11-05
Hello Experts,

I figured it is better to ask, than to do something and screw things up.  I have a bunch of old PHP scripts that I am trying to convert to new coding.  Can anyone verify for me the preg_replace() equivalents to the following old PHP functions:

$Output = ereg_replace("[\"]", "\\\"", $Output);
$Output = ereg_replace("'", "`", $Output);
ereg_replace("[\]", "/", $Output);

Open in new window


Also, since I have your attention, can you verify the following convertion to preg_match():
eregi("maxrow", $Output);

Open in new window


Thanks in advance for your assistance in this matter.
0
Comment
Question by:OmniUnlimited
  • 6
  • 4
  • 2
12 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 39625750
eregi("maxrow") == preg_match('/maxrow/i')
0
 
LVL 17

Author Comment

by:OmniUnlimited
ID: 39625754
Ray, thanks, that's great, but how about the other 3?
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 39625756
I _think_ this may be right...
ereg_replace("[\"]", "\\\"", $Output); == str_replace('"', "\\\"", $Output);
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 39625760
$Output = ereg_replace("'", "`", $Output); == $Output = str_replace("'", "`", $Output);
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 84 total points
ID: 39625770
$Output = ereg_replace("[\"]", "\\\"", $Output);
$Output = ereg_replace("'", "`", $Output);
ereg_replace("[\]", "/", $Output);

Open in new window

Should be the same as:
$Output = preg_replace("/[\"]/", "\\\"", $Output);
$Output = preg_replace("/'/", "`", $Output);
preg_replace("/\\\\/", "/", $Output)

Open in new window


eregi("maxrow", $Output);

Open in new window

is the same as:
preg_match("/maxrow/i", $Output);

Open in new window

0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 39625772
ereg_replace("[\]", "/", $Output); == str_replace('\', '/', $Output);

The right way to work this issue is to create the test data, so you can test as you modify the regular expressions.

The general pattern that works for most ereg-to-preg replacements is to provide a regex delimiter before and after the expression and to add the appropriate flags, usually "i" for case-insensitive comparisons.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39625789
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39625798
str_replace (and str_ireplace), as Ray provided, is a better (faster, and simpler) function to use for such simple replacements, but I've provided the preg_replace equivalent anyway for your interest.
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 84 total points
ID: 39625808
I'd write the str_replace's like this:
$Output = str_replace('"', '\"', $Output);
$Output = str_replace("'", '`', $Output);
str_replace("\\", '/', $Output);

Open in new window

I tested these, and they work fine.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39625877
Whenever I see code that looks like this, I think there may be a backstory.  Perhaps the original author did not understand MySQL_Real_Escape_String() or AddSlashes() ?  This is a code smell...

<?php // RAY_temp_terryatopus.php
ini_set('display_errors', TRUE);
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA
$old = <<<EOD
Double "quotes" in this string.
EOD;

$new = str_replace('"', '\"', $old);

echo htmlentities($old);
echo PHP_EOL;
echo htmlentities($new);

Open in new window

Outputs:
Double "quotes" in this string.
Double \"quotes\" in this string.

Where would anyone use the output string?  If it's in a query, the REGEX is inadequate because the quote marks are only part of the things that need to be escaped, and the expression as written does not take into account the character set of the DB connection.  Maybe it's time to refactor!

Best to all, over and out, ~Ray
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 39625912
Good point Ray!
0
 
LVL 17

Author Closing Comment

by:OmniUnlimited
ID: 39625967
Many thanks Experts for your outstanding help.

Ray, thanks for your comments concerning the need to refactor.  Yes, unfortunately, this is one of those situations where I inherited some old code that may or may not have some significant issues.  In such cases, we just try and stem the tide of problems until a more permanent solution can be found.

Thanks again to all.  Your assistance is greatly appreciated.

Best Regards,

Jason
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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

14 Experts available now in Live!

Get 1:1 Help Now