...

View Full Version : BB Code



stophon4
04-28-2004, 12:55 AM
I need to do a BB code thing for a guestbook I am making. I also need to disable html. I don't really know where to start. I know I need to do something like:


preg_replace($_POST['comments'], "[b]", "<b>")


But that doesn't work. I have an idea with that. But I have absolutely no idea how to disable html...
How would I do this?

All help is apreciated :thumbsup:

note: I tried searching the forum but it said that BB was too short...

SDP2006
04-28-2004, 02:26 AM
If you want to disable all HTML, just use strip_tags();

Mhtml
04-28-2004, 02:31 AM
I like htmlentities() better...



$data = preg_replace("/\[b\]/","<strong>",$data);
$data = preg_replace("/\[\/b\]/","</strong>",$data);


Simple enough, follow that pattern:).. You could optimize it more, make it pull the patterns from mysql or text file and have dynamic bb even.

stophon4
04-28-2004, 08:10 PM
I like htmlentities() better...



$data = preg_replace("/\[b\]/","<strong>",$data);
$data = preg_replace("/\[\/b\]/","</strong>",$data);


Simple enough, follow that pattern:).. You could optimize it more, make it pull the patterns from mysql or text file and have dynamic bb even.

What are htmlentities() ?

missing-score
04-28-2004, 08:43 PM
Take a look at http://www.php.net/htmlentities

Mhtml
04-28-2004, 08:58 PM
It's a good idea to grab the CHM version of the PHP reference as well, saves lots of time. :)

stophon4
04-29-2004, 10:46 PM
I did look that up, I just couldn't understand it...

Mhtml
04-29-2004, 10:53 PM
The htmlentities() function?

Ok, say you have a string with html in it .. You might want for whatever reason to strip the html so that it just becomes plain text. htmlentities does this for you!


$string = "<script>while(1)alert('this is a malice natured script that someone could post in a forum or similar that allowed html!');</script>";

$string = htmlentities($string);
echo $string;

Now, the string which you echo will now look like -
<script>while(1)alert('this is a malice natured script that someone could post in a forum or similar that allowed html!');</script>

Rather than being real html which would continually popup alert boxes ;) .. Which is why most places strip html, or at least the script tags.

stophon4
04-30-2004, 03:04 PM
Thanks, it works great. It is just the manual that php.net gives you can be hard for me to understand, I usually go there before I go to here though, I am just not expierenced enough to really understand how they do that :thumbsup:

missing-score
04-30-2004, 07:23 PM
yep it usually takes a while to get used to the documentation... I find it helps to read the page (so you can find out what the function does and what arguments it has ect), then have a go at the examples they provide in a test php file. (not saying theres any problem with you posting here, just trying to help you find a faster solution in the future :))

stophon4
04-30-2004, 09:07 PM
Thanks, I have a new thing that works great, except there is a problem, and I have no idea what I am doing worong or where it would say in the manual...

I have a break tag ([break]) that I preg_replace to <br> and it works except all text after [break] goes on a new line, not the same on, I have absolutely no idea why...

missing-score
04-30-2004, 09:23 PM
hmm... im not sure I quite understand your last post... If you replace [break] with <br> then the code will go on a new line becuase the <br /> tag starts a newline...

stophon4
04-30-2004, 09:27 PM
no, the source, not the text itself...

Example:
it prints this


code....<br>
hello

instead of


code...<br>hello


Maybe that will clear things up...

missing-score
04-30-2004, 09:34 PM
hmm... It may be that when you input text, you are leaving a newline before putting the [break] tag.

eg:



Blah....
[break]
Blah...


This means that your return spaces are actually being sent as \r\n (or just \n if you are on linux), and in the source this will appear as a newline. You could consider using nl2br ( http://www.php.net/nl2br ) to replace newlines with <br /> tags?

Mhtml
05-01-2004, 12:41 AM
Can I see your preg_replace call for the [break] code, perhaps there is something wrong with it a little.

stophon4
05-01-2004, 01:34 AM
$_POST['comments'] = preg_replace("/\[break\]/","<br>",$_POST['comments']);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum