Page 1 of 1

how to make bbcodes

Posted: Wed Jul 08, 2009 11:30 am
by Naeem
I have made a comment page and want to use a bbcode tags in it i have tried too much but all in vain.
it's a simple method to turn html tags into bbcode:
here is a code:

Code: Select all

<?php
$str = "<b>Hello World</b>";
$pattern = "/(<)(bB)([^>]*>)/e";
$str = preg_replace($pattern,"'\\1'.'[b]'.'\\3'",$str);
echo $str;
?>
But i want to use it in forms when user submit his information he could use bbodes insted of html tags.
And here is my html form in which i am trying to post data with bbcodes:

Code: Select all

<html>
<head><title>Comment Form</title></head>
<style type="text/css">
body {
    border: 2% solid #dddddd;
    margin: 4%;
    padding: 4%;    
}
.button {
    border:1px inset #dddddd;
}
</style>
<body>
<center><form method="post" action="form.php">
<table border="1" cellpadding="2" cellspacing="0" width="75%" style="border-collapse:collapse;border-bottom:1px solid #dddddd;border-left:2px solid #ccc;border-right:2px solid #ccc;border-top:1px solid #dddddd;">
<tr><td><b>Enter Comments:</b></td><td><textarea name="comments"></textarea></td></tr>
<tr><td>&nbsp;</td><td><input type="submit" value="Submit" class="button"/>&nbsp;<input type="reset" value="Reset" class="button"/></td></tr>
</table>
</form></center>
</body>
</html>

Re: how to make bbcodes

Posted: Wed Jul 08, 2009 11:46 am
by JKM

Re: how to make bbcodes

Posted: Wed Jul 08, 2009 11:55 am
by Naeem
Thanks JKM
but can you please teach me a simple way to change <b> into for this form which i have provided above so i could understand.
Thanks again. :)

Re: how to make bbcodes

Posted: Thu Jul 09, 2009 12:28 am
by Naeem
Please any body...... :cry:

Re: how to make bbcodes

Posted: Thu Jul 09, 2009 12:36 am
by SvanteH

Code: Select all

<?php
  $data = "hello [b]bold[/b] text here!";
  $data = preg_replace("/\[b\](.+?)\[\/b\]/s", "<b>$1</b>", $data);
  echo $data;
?>

Re: how to make bbcodes

Posted: Thu Jul 09, 2009 1:51 am
by Naeem
SvanteH wrote:

Code: Select all

<?php
  $data = "hello [b]bold[/b] text here!";
  $data = preg_replace("/\[b\](.+?)\[\/b\]/s", "<b>$1</b>", $data);
  echo $data;
?>
Thanks SvanteH :)
Please teach me how it works.

Re: how to make bbcodes

Posted: Thu Jul 09, 2009 2:01 am
by SvanteH
Instead of me trying to explain what every character does I think you'll learn more by visiting,
viewtopic.php?f=38&t=33147