views:

1183

answers:

6

How do I, using preg_replace, replace more than one underscore with just one underscore?

A: 

preg_replace('/[_]+/', '_', $your_string);

chelmertz
+11  A: 

The + operator matches multiple instances of the last character (or capture group).

$string = preg_replace('/_+/', '_', $string);
soulmerge
A: 

I'm don't the reasons you want to use preg_replace but what's wrong with:

str_replace('__', '_', $string);
RMcLeod
Because it does not replace `___` (i.e. 3 underscores) with `_`.
soulmerge
You would have to apply this solution recursively in order to work on 3 or more underscores.
Ben James
disclaimer: not downvoted
soulmerge
My mistake read it as just replacing __ as opposed to any number of underscores.
RMcLeod
Your code will do `___`(3) -> `__`(2) and **not** `___`(3) -> `_`(1) and so on,
Stefan Gehrig
A: 

preg_replace()

the + operator is needed

$text = "______";
$text = preg_replace('/[_]+/','_',$text);
Peter Lindqvist
No need to define a character class.
gnud
So true, but i do it anyway. And it would seem i'm not alone.
Peter Lindqvist
+2  A: 

Running tests, I found this:

while (strpos($str, '__') !== false) {
    $str = str_replace('__', '_', $str);
}

to be consistently faster than this:

$str = preg_replace('/[_]+/', '_', $str);

I generated the test strings of varying lengths with this:

$chars = array_merge(array_fill(0, 50, '_'), range('a', 'z'));
$str = '';
for ($i = 0; $i < $len; $i++) {  // $len varied from 10 to 1000000
    $str .= $chars[array_rand($chars)];
}
file_put_contents('test_str.txt', $str);

and tested with these scripts (run separately, but on identical strings for each value of $len):

$str = file_get_contents('test_str.txt');
$start = microtime(true);
$str = preg_replace('/[_]+/', '_', $str);
echo microtime(true) - $start;

and:

$str = file_get_contents('test_str.txt');
$start = microtime(true);
while (strpos($str, '__') !== false) {
    $str = str_replace('__', '_', $str);
}
echo microtime(true) - $start;

For shorter strings the *str_replace()* method was as much as 25% faster than the *preg_replace()* method. The longer the string, the less the difference, but *str_replace()* was always faster.

I know some would prefer one method over the other for reasons other than speed, and I'd be glad to read comments regarding the results, testing method, etc.

GZipp
+3  A: 

Actually using /__+/ or /_{2,}/ would be better than /_+/ since a single underscore does not need to be replaced. This will improve the speed of the preg variant.

Cheatah