Solved

ereg_replace() to preg_replace()

Posted on 2013-11-05
12
292 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 109

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 109

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 109

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 109

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
 
LVL 109

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 109

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

840 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