Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ereg_replace() to preg_replace()

Posted on 2013-11-05
12
Medium Priority
?
320 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 total points
ID: 39625756
I _think_ this may be right...
ereg_replace("[\"]", "\\\"", $Output); == str_replace('"', "\\\"", $Output);
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 111

Assisted Solution

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

Accepted Solution

by:
Terry Woods earned 336 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 664 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 336 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 111

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
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 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 …

715 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