Solved

ereg_replace() to preg_replace()

Posted on 2013-11-05
12
285 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
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.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

777 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