开发者

Replacing multiple `-` with one `-` without regexp

开发者 https://www.devze.com 2022-12-25 17:34 出处:网络
I\'ve seen so many misuses of RegExp, I don\'t really like it :) I have string (as a result of two str_replaces) that might look something like this:

I've seen so many misuses of RegExp, I don't really like it :)

I have string (as a result of two str_replaces) that might look something like this:

.?Th开发者_StackOverflow社区is iš my ".stRiNg."!
          |
          V
--this-is-my---string---

Is there any way better than

$string = trim(preg_replace('/[-]+/u','-', $string),'-');

to get:

this-is-my-string

?


preg_replace() wins

<?php

function benchmark($callback){
  echo sprintf('%-30s: ', $callback);
  $t = microtime(true);
  foreach(range(1, 10000) as $n){
    call_user_func($callback);
  }
  echo (microtime(true)-$t)."\n";
}

function implode_explode_filter(){
  implode('-', array_filter(explode('-', '--this-is-my---string---')));
}

function preg_replace_trim(){
  preg_replace('/-+/', '-', trim('--this-is-my---string---', '-'));
}

function brant(){
  $parts = explode("-",'--this-is-my---string---');
  for($i=0;$i<count($parts);$i++) {
      if (strlen($parts[$i]) < 1) {
          unset($parts[$i]);
      }
  }
  reset($parts);
  $string = implode("-",$parts);
}

function murze_bisko(){
  $string = "--this-is-my---string---";
  while (strpos($string, "--") !== false) {
    $string = str_replace("--", "-", $string);
  }
  $string = trim($string, '-'); # both of their answers were broken until I added this line
}

benchmark('implode_explode_filter');
benchmark('preg_replace_trim');
benchmark('brant');
benchmark('murze_bisko');

# Output
# implode_explode_filter        : 0.062376976013184
# preg_replace_trim             : 0.038193941116333
# brant                         : 0.11686086654663
# murze_bisko                   : 0.058025121688843
?>


I don't understand why you are looking for a 'better' way. Your way uses a simple regex in a place where it's perfectly appropriate to do so. What could be better than that?


print implode('-', array_filter( explode( '-', $string) ) );

Easy. :)


A better way? Probably not. Another way? Yes (essentially one-line version of Brant's answer):

implode('-', array_filter(explode('-', $string), function($s) { return strlen($s); }));

Note that you can't just use the naked array_filter reliably, because !!"0" == false in PHP.


$parts = explode("-",'--this-is-my---string---');
for($i=0;$i<count($parts);$i++) {
    if (strlen($parts[$i]) < 1) {
        unset($parts[$i]);
    }
}
reset($parts);
$string = implode("-",$parts);


So you want to go from

--this-is-my---string---

to

this-is-my-string?

The best way would be a reg exp, but if you want a simple solution in php you can use

$string = "--this-is-my---string---";
while (! strpos($string, "--") === false) {
  $string = str_replace("--", "-", $string);
}

After the loop $string will contain the result.


Just for the sake of the competition:

$string = str_replace('--','-', str_replace('--','-', '--this-is-my---string---'));

This is the fastest way to do it. It first replaces double -- with single and then cleans the rest again, so only single - remain.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号