doushi4956 2016-05-22 02:00
浏览 68
已采纳

在PHP函数中从tweet中检索所有用户名

I have this function, which color every @username contained in a string

 //color  @username
 function hashtag_links($string,$id_session) {
 preg_match_all('/@(\w+)/',$string,$matches);
 foreach ($matches[1] as $match) {
$string = str_replace("@$match", "<span class=color>@$match</span>", "$string");
 }
 return $string;
 }

Everything is good while the usernames are different (@cat,@pencil,@scubadiving), but in case the username starts with the same letters (@cat,@caterpiller,@cattering), the functions only colors the repeated letters in this case (@cat), what to do?

  • 写回答

2条回答 默认 最新

  • duanpai1033 2016-05-22 02:18
    关注

    Use preg_replace instead:

    //color  @username
    function hashtag_links($string,$id_session) {
        return preg_replace('/@(\w+)/', '<span class=color>@$1</span>', $string);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式