Search code examples
phpregexhyperlinkpreg-replacebbcode

Convert url string wrapped in BBcode-style [url] and [/url] tags into an HTML hyperlink


I want to convert [url]http://www.stackoverflow.com[/url] to:

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

I assume that I want to use preg_replace(), but I just can't get it working.


Solution

  • $str = preg_replace('/\[url\](.*?)\[\/url\]/', '<a href="$1">$1</a>', $str);
    

    CodePad.

    PHP does have a BBCode parser too.

    BBCode sucks... have you tried Markdown?