BB code

Ask about a PHP problem here.
Post Reply
white04004
Posts: 4
Joined: Tue Dec 06, 2011 5:41 pm
Contact:

BB code

Post by white04004 »

Hi guys!

I am facing another problem again. I am developing my custom blog system, and I want to add [url]www.google.com[/url] and convert it into <a href="www.google.com">www.google.com</a>. How can I do that? I had tried substr(); but it doesn't work to url. Please help me. Thanks in advance.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: BB code

Post by Temor »

white04004 wrote:Hi guys!

I am facing another problem again. I am developing my custom blog system, and I want to add http://www.google.com and convert it into <a href="www.google.com">www.google.com</a>. How can I do that? I had tried substr(); but it doesn't work to url. Please help me. Thanks in advance.
What you want is BBcode. Pretty similar or identical to the one this forum uses, am I right?

Google BBCode and there are tonnes of free scripts and tutorials on how to implement them.
bowersbros
Posts: 534
Joined: Thu May 05, 2011 8:19 pm

Re: BB code

Post by bowersbros »

I think, since he is working on his own custom software, that he is doing it for practice. Therefore he would want to make it himself.

try something like this:
<?php

$string = "http://google.com/";
$match = preg_match("/^(|[url=])((https?|ftp|gopher|telnet|file ... -=\\\.&]*)/^","<a href='".$1."'>{$1}</a>",$string);
echo $match;
Try something like that, untested though.
I don't like to brag, but I wasn't circumcised. I was circumnavigated. ;)

Want to learn something new? Or maybe reinforce what you already know? Or just help out? Please subscribe to my videos: http://goo.gl/58pN9
User avatar
EcazS
Posts: 789
Joined: Fri May 06, 2011 5:29 pm

Re: BB code

Post by EcazS »

http://www.pixel2life.com/forums/index. ... ge__st__20
There are a lot of BB Parser code snippets that you can try out yourself.
Chathu94
Posts: 2
Joined: Sat Dec 24, 2011 1:44 am

Re: BB code

Post by Chathu94 »

For any url :
$str = "your url here";

$str = preg_replace("\[[uU][rR][lL]\](.*?)\[/[uU][rR][lL]\]", "<a Href='$1'>$1</a>" ,$str);
Last edited by jacek on Sun Jan 01, 2012 1:19 am, edited 2 times in total.
Reason: code tags...
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: BB code

Post by jacek »

You can use the i modifier to make the expression case-insensitive
$str = preg_replace("#\[url\](.*?)\[/url]#i", "<a href='$1'>$1</a>" ,$str);
bit neater :)
Image
white04004
Posts: 4
Joined: Tue Dec 06, 2011 5:41 pm
Contact:

Re: BB code

Post by white04004 »

Thanks for your information. It works perfectly for me. Thanks a lot.
Post Reply