The most efficient way to truncate a string using preg_replace?

I was looking for some code and started thinking about the most efficient way to trim the string (in this case, the URI) with preg_replace .

First of all, I understand that using preg_replace in the first place can be redundant for this task, that it can be uselessly expensive and that it is better to handle it with PHP-friendly functions like substr . I know it.

However, consider these two different regular expressions:

 $uri = '/one/cool/uri'; // Desired result '/one/cool' // Using a back-reference $parent = preg_replace('#(.*)/.*#', "$1", $uri); // Using character class negation $parent = preg_replace('#/[^/]+$#', '', $uri); 

By default, I would suggest that in the first case, creating a backlink would be more expensive than not doing it, and so a second example would be preferable. But then I began to wonder if using [^/] in the second example could be more expensive than the corresponding one . in the first example, and if so, how much more?

I prefer the first example in terms of readability, and since we split the hair, I tend to choose it between them (in the end, there is also value for writing readable code there). Maybe just my personal preferences.

Thoughts?

+4
source share
1 answer

I will also measure the running time of both options. This document information may also help:

http://www.php.net/manual/en/regexp.reference.performance.php

If you use such a pattern with subject lines that do not contain newline, the best performance is achieved by setting PCRE_DOTALL or the beginning of the pattern with the ^ character. * to indicate explicit binding. This saves PCRE from scanning for a subject that is looking for a new line to restart.

So $parent = preg_replace('#^(.*)/.*#s', "$1", $uri); can speed up the first option. The second option is not needed:

s (PCRE_DOTALL)

If this modifier is set, the dot metacharacter in the pattern matches all characters, including newlines. Without this, newlines are excluded. This modifier is equivalent to the Perl / s modifier. A negative class, such as [^ a], always matches a newline character , regardless of the setting of this modifier.

+2
source

All Articles