1

I need a regex to remove the # from a twitter name.

Tried this:

$name = '#mytwitter';
$str = preg_replace('/\#/', ' ', $name);

Sure this is a simple fix , but Google didn't help. Thanks!

BobFlemming
  • 2,040
  • 11
  • 43
  • 59

4 Answers4

6

You don't need to use preg_replace, just use str_replace:

str_replace('#','',$name);
fredley
  • 32,953
  • 42
  • 145
  • 236
2

Why are you escaping #?

$name = '#mytwitter';
$str = preg_replace('/#/', ' ', $name);

Edit: Your original code does work, too. Note that preg_replace returns the substituted string but doesn't change the original. The value of $str is " mytwitter".

Tim
  • 13,904
  • 10
  • 69
  • 101
  • `preg_replace` is much slower than `str_replace`, there is no need to use it here – JamesHalsall Mar 10 '11 at 17:23
  • @Jaitsu: Certainly, but there is always a trade-off between fixing and rewriting the code. It is not obvious at all which the original poster would benefit the most from. – Tim Mar 10 '11 at 17:43
1

You do not need to escape #.

$str = preg_replace('/#/', '', $name);

However, for a simple character removal, you are better off using str_replace(). It's faster for those situations.

$str = str_replace('#', '', $name);
Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
1

I would recommend using strtok for this, as it's more performant. Just use it like so:

$str = strtok('#mytwitter', '#');

Here are some benchmarks i just ran (50000 iterations):

strreplace execution time: 0.068472146987915 seconds
preg_replace execution time: 0.12657809257507 seconds
strtok execution time: 0.043070077896118 seconds

The script i used to benchmark is (taken from Beautiful way to remove GET-variables with PHP?):

<?php

$number_of_tests = 50000;

// str_replace
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

for($i = 0; $i < $number_of_tests; $i++){
    $str = "#mytwitter";
    $str = str_replace('#' , '', $str);
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "strreplace execution time: ".$totaltime." seconds;  <br />";

// preg_replace
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

for($i = 0; $i < $number_of_tests; $i++){
    $str = "#mytwitter";
    $str = preg_replace('/#/', ' ', $str);
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "preg_replace execution time: ".$totaltime." seconds;  <br />";

// strtok
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;

for($i = 0; $i < $number_of_tests; $i++){
    $str = "#mytwitter";
    $str = strtok($str, "#");
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "strtok execution time: ".$totaltime." seconds; <br />";

  [1]: http://php.net/strtok
Community
  • 1
  • 1
alexn
  • 57,867
  • 14
  • 111
  • 145