dongxiao9583 2012-03-28 02:31
浏览 145
已采纳

如何制作一个bbcode来将url标签解析为链接?

How should I go about parsing a url in php? I wanna make it so it goes

[url=http://www.google.com]Google[/url]

Turns into:

<a href="http://www.google.com">Google</a>

This is the code I'm using for my other bb codes:

function postparser($post){ 
        $post = str_replace("
",'END_OF_LINE',$post);

        $post = str_replace("[line]",'HORIZONTAL_LINE',$post);

        $post = str_replace("[bold]",'BOLD_TEXT_START',$post);
        $post = str_replace("[/bold]",'BOLD_TEXT_END',$post);

        $post = str_replace("[yt]",'YOUTUBE_START',$post);
        $post = str_replace("[/yt]",'YOUTUBE_END',$post);

        $post = sanitize($post);

        $post = str_replace("END_OF_LINE",'<br />',$post);

        $post = str_replace("HORIZONTAL_LINE",'<hr />',$post);

        $post = str_replace("BOLD_TEXT_START",'<b>',$post);
        $post = str_replace("BOLD_TEXT_END",'</b>',$post);

        $post = str_replace("YOUTUBE_START",'<iframe width="560" height="315" src="http://www.youtube.com/embed/',$post);
        $post = str_replace("YOUTUBE_END",'" frameborder="0" allowfullscreen></iframe>',$post);

        return $post;
    }

How would I go about doing this?

  • 写回答

1条回答 默认 最新

  • douyao4632 2012-03-28 02:42
    关注
    $post = preg_replace('/\[url=(.+?)\](.+?)\[\/url\]/', '<a href="\1">\2</a>', $post);
    

    That will turn: [url=http://google.com]Google[/url]

    Into parsed bbcode text: Google

    You'll probably want to use more specific regex than just .+ to filter out potentially bad/dangerous input.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?