Please convert my ereg_replace to preg_replace

hankknight used Ask the Experts™
What would the preg_replace code be for this?
if($item == 'lang' && @is_dir("$folder/$item") && ereg_replace("/[^/]+/\\.\\.","",$folder.'/..') == Director::baseFolder()) continue;

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try this
if($item == 'lang' && @is_dir("$folder/$item") &&  preg_replace( '#/[^/]+/\.\.#', '', $folder.'/..' ) == Director::baseFolder()) continue;

Open in new window

or (folded up a bit)

if ($item == 'lang' && 
   @is_dir("$folder/$item") &&  
    preg_replace( '#/[^/]+/\.\.#', '', $folder.'/..' ) == Director::baseFolder()

Open in new window

Most Valuable Expert 2011
Top Expert 2016
I think the first thing I would do is get rid of those compound statements.  The REGEX conversion from ereg() to preg() will be a lot easier if your code is written in a way that enables you to change a line at a time.

Generally speaking, you can change most ereg() patterns into preg() patterns by simply adding a REGEX delimiter front and back.

Given this:

The contents would look something like this:
$e_regex = '/[^/]+/\\.\\.';
$p_regex = '#' . $e_regex . '#';

And then you would write something like this:
preg_replace($p_regex, NULL, ...

And there are the eregi functions that are case-insensitive  To handle those, you would add a REGEX modifier to the end of the expression after the ending REGEX delimiter.  Example:
p_regex = '#' . $e_regex . '#i';

HTH, ~Ray

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial