0

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.

3

2 Answers 2

3
$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?

Sign up to request clarification or add additional context in comments.

1 Comment

I'm not a huge fan of BBCode, but I really only needed barebones functionality so it works well in this case. I wasn't aware of Markdown, so thanks for the heads up.
1

Try this:

$text = preg_replace("/\[url\](.*?)\[\/url\]/i","<a href='$1'>$1</a>", "[url]http://www.stackoverflow.com[/url]");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.