doujian0265 2010-04-14 19:27
浏览 8

当url包含主题标签时,如何让preg替换为work

I found a function online for turning a url within a string into a clickable link. However, when the url contains a hashtag it doesn't work. eg. http://www.bbc.co.uk/radio1/photos/fearnecotton/5759/1#gallery5759

Here's the part of the function concerned:

$ret = preg_replace(
    "#(^|[
 ])([\w]+?://[\w]+[^ \"
\t< ]*)#",
    "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>",
    $ret
);

$ret = preg_replace(
    "#(^|[
 ])((www|ftp)\.[^ \"\t
< ]*)#",
    "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>",
    $ret
);

Any ideas? thanks

  • 写回答

1条回答 默认 最新

  • dongmen9517 2010-04-15 12:50
    关注

    Try this:

    <?php
    
    $text = "This is my link:  http://www.bbc.co.uk/radio1/photos/fearnecotton/5759/1#gallery5759";
    $text = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]","<a href=\"\\0\" target=\"_blank\">\\0</a>", $text); 
    echo $text; // output: This is my link:  <a href="http://www.bbc.co.uk/radio1/photos/fearnecotton/5759/1#gallery5759" target="_blank">http://www.bbc.co.uk/radio1/photos/fearnecotton/5759/1#gallery5759</a>
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答