0

I need to convert 4 ereg_replace expressions to preg_replace. I've been trying various combinations based on tutorials and not getting too far with getting my site to work.

Can some kind soul assist me in resolving these?

$path = ereg_replace('\.[\.]+', '', $path); // remove any '..' (jumping up a directory)
$path = ereg_replace('/[/]+', '/', $path);
$back_url = ereg_replace('dir=[^\&]*', "dir=$back_directory", $this->current_url);
$dir_url = ereg_replace('dir=[^\&]*', "dir=$dirpath", $this->current_url);

Thanks

kapa
  • 77,694
  • 21
  • 158
  • 175
lokust
  • 51
  • 1
  • 6
  • Could you show us what you have tried and what is the problem? – kapa Aug 09 '12 at 12:03
  • Here is a link to the [PCRE: Differences from POSIX regex](http://www.php.net/manual/en/reference.pcre.pattern.posix.php) manual page. – kapa Aug 09 '12 at 12:04
  • I have added forward slashes to the expressions, eg: `$path = ereg_replace(/'\.[\.]+/', '', $path); $path = ereg_replace('//[/]+', '//', $path);` I've validated these with an online regex checker and it points out various syntax issues so I've corrected them so they pass the checker however when implemented they still fail. – lokust Aug 09 '12 at 12:10

1 Answers1

0

In order to convert ereg to preg, you have to surround your regex with delimiters.
The most common is / but you can use approx any character except spaces.
You have also to take care that you haven't the chosen delimiter in the regex, in this case, you have to escape these characters or use another delimiter.

With your examples:

ereg_replace('\.[\.]+', '', $path);

becomes

preg_replace('/\.[.]+/', '', $path); 

or

preg_replace('/\.{2,}/', '', path);

and

ereg_replace('/[/]+', '/', $path);

becomes

preg_replace('/\/[\/]+/', '/', $path);

or

preg_replace('~/[/]+~', '/', $path);

or

preg_replace(':/{2,}:', '/', $path);
Toto
  • 89,455
  • 62
  • 89
  • 125