CodeIgniter Forums
rtrim vs. preg_replace vs. ereg_replace - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: rtrim vs. preg_replace vs. ereg_replace (/showthread.php?tid=7723)



rtrim vs. preg_replace vs. ereg_replace - El Forum - 04-20-2008

[eluser]BorisK[/eluser]
I was checking the file_helper.php code today when line 124 struck me as unnecessarily complex way to trim the slash.

Code:
$path = preg_replace("|^(.+?)/*$|", "\\1", $path);

The same code can be rewriten as:
Code:
$path = rtrim($path,'/');

In this case rtrim command is four times faster than preg_replace and more readable as well.

Of course, ereg_replace performs the worst. Here are benchmarking results for 10m iterations:

rtrim: 0.649 s
preg_replace: 2.868 s
ereg_replace: 5.750 s


rtrim vs. preg_replace vs. ereg_replace - El Forum - 04-20-2008

[eluser]BorisK[/eluser]
Also, in the path_helper.php on line 55, replacing the line that adds a slash to a path

Code:
$path = preg_replace("#([^/])/*$#", "\\1/", $path);

with

Code:
$path = rtrim($path,'/').'/';

speeds things up by about 6-7 times.