如何从 Twitter 标签中删除 #? [英] How to remove # from Twitter hashtags?
本文介绍了如何从 Twitter 标签中删除 #?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从 twitter 的哈希标签中去掉 #,所以:
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);
在线查看:ideone
我还将正则表达式更改为更具体,但为了获得更好的正则表达式,我建议您参考此问题及其答案(对于 .NET,但该想法在 PHP 中是相同的):
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):
这篇关于如何从 Twitter 标签中删除 #?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文