duanre4421 2011-12-16 13:57
浏览 76
已采纳

将HREF链接添加到提及URL的文本(使用preg_replace)

I can see this question has been asked many times before, but I just can't get my particular implementation to work, so wondered if someone can enlighten me of the error of my ways?!

I have a Twitter feed onto my site and (using PHP) want to replace any URLs mentioned in Tweets with the relevant HTML anchor and HREF.

An example Tweet could be ...

No BlackBerry 10 smartphones till Q4 2012: A bad day for BlackBerry fans today after it was revealed that due to... http://t.co/lNEdUUef

I am using the following preg_replace in attempt to alter this ...

$tweet2 = preg_replace('#http://\S+#i', '<a href="$0">$0</a>', $tweet);

Unfortunately this isn't making a blind bit of difference and the text is left unchanged.

Can someone spot what I've done wrong and advise accordingly?

  • 写回答

2条回答 默认 最新

  • doue9730 2011-12-16 14:07
    关注

    Try this

    $tweet2 = preg_replace( '#(http:\/\/[^ ]+)#i', '<a href="$1">$1</a>', $tweet );
    

    It should work as expected

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

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输