BBCode - How to?

Written something you are proud of, post it here.
Post Reply
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

BBCode - How to?

Post by louiegiezer »

how can iapply the bbcode at my script this is wat ive done...

<?php 
include 'core/init.php';
include 'texteditor.php';

if (isset($_POST['user'], $_POST['title'], $_POST['body'], $_POST['cid'])){
	if (empty($_POST['user'])){
		$errors[] = "Please Enter Your Valid Username.";
	}
	
	if (empty($_POST['title'])){
		$errors[] = "Your Title cannot be empty.";
	}
	
	if (empty($_POST['cid'])){
		$errors[] = "Please Enter Your Category.";
	}
	
	if (empty($_POST['body'])){
		$errors[] = "Your Topic Cannot be empty.";
	}

	if (empty($errors)){
	add_post($_POST['user'], $_POST['title'], $_POST['body'], $_POST['cid']);
	header('Location: admin.php');
	die();
	}
}
?>

<?php include 'includes/overall/header.php'; ?>

	<div id="post-topic">
	<?php
		if (empty($errors) === false){
				echo error_box($errors);
			}
		
		?>
	
		<form action="" method="post">
			<p>
				<label>Name</label>
				<input type="text" name="user" id="user">
			</p>
			<p>
				<label>Title</label>
				<input type="text" name="title" id="title">
			</p>
			
			<p>	
				<label>Category</label>
				<select name="cid"><option value='0'>Please Choose</option>
					<?php
					$cat_post = get_cat();
					foreach ($cat_post as $cat){
					?>
					<option value="<?php echo $cat['cat_id'];  ?>"> <?php echo $cat['cat'];  ?></option>
					<?php
					 }
					echo "</select>";
					?>
			
			</p>	
			<p>
			
			<a href="javascript:void(0);" onclick="document.getElementById('body').value += '[\b][\/b]'">Bold</a>
			<textarea cols="100" name="body" id="body" rows="20"></textarea>
			</p>
			<p>
				<input type="submit" value="Add Post">
			</p>	
		</form>
		</div>

<?php include 'includes/overall/footer.php'; ?>
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: BBCode - How to?

Post by Temor »

I split the topic to keep the other thread as clean as possible.

Original topic:
http://betterphp.co.uk/board/viewtopic.php?f=5&t=41


The function posted in there :
function bbcode($text) {
        $text = htmlentities($text);
        $search = array(
                '/\[b\](.*?)\[\/b\]/is',                                
                '/\[i\](.*?)\[\/i\]/is',                                
                '/\[u\](.*?)\[\/u\]/is',
                '/\[s\](.*?)\[\/s\]/is',
                '/\[sub\](.*?)\[\/sub\]/is',
                '/\[sup\](.*?)\[\/sup\]/is',
                '/\[img\](.*?)\[\/img\]/is',
                '/\[url\](.*?)\[\/url\]/is',
                '/\[url\=(.*?)\](.*?)\[\/url\]/is',
                '/\[size\=(.*?)\](.*?)\[\/size\]/is',
                '/\[color=(.*?)\](.*?)\[\/color\]/is',
                '/\[center\](.*?)\[\/center\]/is',
                '/\[right\](.*?)\[\/right\]/is',
                '/\[left\](.*?)\[\/left\]/is',
                '/\[justify\](.*?)\[\/justify\]/is',
                '/\[youtube\](.*?)\[\/youtube\]/is',
                '/\[font\=(.*?)\](.*?)\[\/font\]/is',
                '/\[ul\](.*?)\[\/ul\]/is',
                '/\[ol\](.*?)\[\/ol\]/is',
                '/\[li\](.*?)\[\/li\]/is',
                '/\[code\](.*?)\[\/code\]/is',
                '/\[quote\](.*?)\[\/quote\]/is',
                '[hr]',
                '/\[email\=(.*?)\](.*?)\[\/email\]/is',
                '/\[rtl\](.*?)\[\/rtl\]/is',
                '/\[ltr\](.*?)\[\/ltr\]/is',
                '/\[table\](.*?)\[\/table\]/is',
                '/\[tr\](.*?)\[\/tr\]/is',
                '/\[td\](.*?)\[\/td\]/is',
                '/\[thead\](.*?)\[\/thead\]/is',
                '/\[tbody\](.*?)\[\/tbody\]/is',
                '/\[th\](.*?)\[\/th\]/is',
                '/\[caption\](.*?)\[\/caption\]/is'
                );
 
        $replace = array(
                '<strong>$1</strong>',
                '<em>$1</em>',
                '<u>$1</u>',
                '<del>$1</del>',
                '<sub>$1</sub>',
                '<sup>$1</sup>',
                '<img src="$1" />',
                '<a href="$1">$1</a>',
                '<a href="$1">$2</a>',
                '<font size="$1">$2</font>',
                '<a style=\'color:$1;\'>$2</a>',
                '<center>$1</center>',
                '<div style="text-align:right;">$1</div>',
                '<div style="text-align:left;">$1</div>',
                '<div style="text-align:justify;">$1</div>',
                '<iframe id="ytplayer" type="text/html" width="640" height="360"
src="https://www.youtube.com/embed/$1" frameborder="0" allowfullscreen>',
                '<a style=\'font-family:$1;\'>$2</a>',
                '<ul>$1</ul>',
                '<ol>$1</ol>',
                '<li>$1</li>',
                '<code>$1</code>',
                '<blockquote>$1</blockquote>',
                '<hr />',
                '<a href=\'mailto:$1\'>$2</a>',
                '<div style=\'direction: rtl\'>$1</div>',
                '<div style=\'direction: ltr\'>$1</div>',
                '<table>$1</table>',
                '<tr>$1</tr>',
                '<td>$1</td>',
                '<thead>$1</thead>',
                '<tbody>$1</tbody>',
                '<th>$1</th>',
                '<caption>$1</caption>'
                );
 
        $bb = preg_replace ($search, $replace, $text);
 
        return $bb;
}
is really simple to use.
$string = "[ b]Bold[ /b]";


echo bbcode($string);
will echo "<strong>Bold</strong"

It really just takes whatever you put in there, looks for bbcode tags ( for example ) and changes those into html tags.
Not much more to it than that :)
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

Re: BBCode - How to?

Post by louiegiezer »

<a href="javascript:void(0);" onclick="document.getElementById('body').value += '[\b][\/b]'">Bold</a>
<textarea cols="100" name="body" id="body" rows="20"></textarea>

like this sir?
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: BBCode - How to?

Post by Temor »

I'm not entirely sure what you're trying to do or why it uses javascript and html. You need neither.
All you need is php.

You pass in a string, say an input field or a textarea.
<form method="post">
<textarea name="bbcode">This is text</textarea>
<input type="submit" value="submit">
</form>
The value of your $_POST['bbcode'] variable will be "This is text". As you would expect.
If you then Echo that variable:
echo $_POST['bbcode'];
you will get:

This is text

If you would want to apply bbcode to this, you would just simply use the function on the $_POST['bbcode'] variable.
$_POST['bbcode'] = bbcode($_POST['bbcode']);
If you now put bbcode inside the variable, it will change it to html characters instead.

<form method="post">
<textarea name="bbcode">[ b]This is text[/ b]</textarea>
<input type="submit" value="submit">
</form>
if you echo this as is, it would print:
[ b]This is text[ /b]

but the bbcode function takes the [ b] and [ /b] tags and swaps them for the equivalent html tags, in this case <strong> and </strong>

so if you now use the bbcode function:
$_POST['bbcode'] = bbcode($_POST['bbcode']);
echo $_POST['bbcode'];
you will get:

<strong>This is text</strong>

which is valid HTML and will show This is text in bold.



*Footnote:
I had to put spaces in the BBcode i showed here or else it wouldn't show the actual tags, but instead make the text bold :)
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

Re: BBCode - How to?

Post by louiegiezer »

thank you sr temor it's working... and it would be nice to add some javascript let say i highlight the text now it will wrap automatically the text with this [ b]text[/b]... anyway thanks again 4 helping me always... :D
ScTech
Posts: 92
Joined: Sat Aug 24, 2013 8:40 pm

Re: BBCode - How to?

Post by ScTech »

It's an absolute pain to get wrapping to work on mobile and IE. I can understand why mobile, but not why people still use IE...
<?php while(!$succeed = try()); ?>
User avatar
louiegiezer
Posts: 57
Joined: Fri Oct 21, 2011 11:31 am
Contact:

Re: BBCode - How to?

Post by louiegiezer »

its 4 personal purposes only so its ok...
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: BBCode - How to?

Post by Temor »

ScTech wrote:It's an absolute pain to get wrapping to work on mobile and IE. I can understand why mobile, but not why people still use IE...
It's a pain, yes- But not impossible!

As for IE, I just reinstalled windows on my pc, and I really CBA installing another browser, so even though I HATE IE, I'm still using it. For now... :P
louiegiezer wrote:thank you sr temor it's working... and it would be nice to add some javascript let say i highlight the text now it will wrap automatically the text with this [ b]text[/b]... anyway thanks again 4 helping me always... :D
Just create a new topic in the javascript forum and I'd be happy to help again :)
Post Reply