douzao5487 2012-02-05 22:16
浏览 171
已采纳

如何从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?

  • 写回答

3条回答 默认 最新

  • duaiwo9093 2012-02-05 22:19
    关注

    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):

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色