Solved

ereg_replace() to preg_replace()

Posted on 2013-11-05
12
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 110

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 110

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 110

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 110

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 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 110

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

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…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to count occurrences of each item in an array.
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 …

628 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