I want to strip off the # from twitter hash-tags, so:
Input: I love #winter and #ice-skating
Output: I love winter and ice-skating
I thought this would do the trick, but it doesn't:
$tweet = preg_replace('/#[^\s]*/i', '${1}', $tweet);
Could somebody explain to me why not? What should I change?
You need to surround the part you want to capture in parentheses:
$tweet = preg_replace('/#([\w-]+)/i', '$1', $tweet);
See it working online: ideone
I also changed the regular expression to be more specific, but for an even better regular expression I refer you to this question and its answers (for .NET but the idea is the same in PHP):
1
in the replacement string with curly braces$tweet = preg_replace('/#([^\s]*)/', '$1', $tweet);
Faster solution:
$tweet = str_replace('#', '', $tweet)
No regex required