Jump to content

Help to create a forum - BBCodes


gaza165

Recommended Posts

Hi all i am using BBCodes on my website to filter out html entities in my blog

 

http://www.thedesignmonkeys.co.uk/

 

Just like PHP Freaks does I want to be able to submit PHP Code to my blog. I want to know the class I need to construct for me to be able to have php code parsed from my blog and to have it coloured like PHP Freaks does using the [ code ] tags.

 

Not sure if this is easy or hard but any guidance would be very useful.

 

Thanks

 

Garry

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/
Share on other sites

  • Replies 61
  • Created
  • Last Reply

Made this if it helps?

<?php
if(isset($_POST['sub']))
{
$str = $_POST['test'];
$str = "<?php ".$str."\n?>";
highlight_string($str);
}
?>
<form method="post" action="">
<textarea name="test"></textarea><br>
<input type="submit" name="sub" value="Go">
</form>

<?php
//select the blog based on the id
	$result = mysql_query("SELECT * FROM blog WHERE title = '$title'") or die(mysql_error());
	if(mysql_num_rows($result) == 0)
	{
		echo "<h1>The post you are looking for cannot be found--> <a href='./blog'>Back to Blog</a></h1>";//post not 		
		exit;
		}	

	while($row = mysql_fetch_array($result))
		{


		$body = str_replace("","'",$body);
		$body = str_replace('','"',$body);
		$body = str_replace('','"',$body);
		$body = str_replace("\'","'",$body);	
		$body = htmlentities($row['body']); 
		$_SESSION['blog_id'] = $row['id'];

			include_once "init.inc.php";
			require_once 'HTML/BBCodeParser.php';
			$options = @parse_ini_file('BBCodeParser.ini');
			$parser = new HTML_BBCodeParser($options);			

		$blog_id = $row['id'];

			echo "<div id='fullblog'>";
			echo "<h1>".$row['title']."</h1>";
			echo "<p class='date'>Posted by ".ucfirst($row['name'])." | ".date("F j, Y",strtotime($row['blog_created']))."</p>";

			$blog_title =  $row['title'];
			$_SESSION['title'] = $blog_title;

			// Pattern and pregmatches etc \\

			$pattern = "%http://\S+\.(?:jpe?g|png|gif)%i";
			$match = preg_match_all($pattern, $body, $matches,1);
			$gaz = $body;	
			$i = 0;	
				foreach($matches[0] as $url) {	
					$replacement = "%TOKEN".$i."%";	
					$gaz = preg_replace("@$url@", $replacement, $gaz, 1);
					$i++;
				}						

			$mpgpattern = "%http://\S+\.(?:mpg)%i";
			$mpgmatch = preg_match_all($mpgpattern, $body, $mpgmatches,1);	
			$m = 0;	
				foreach($mpgmatches[0] as $mpgurl) {	
					$replacement = "%MPG".$m."%";	
					$gaz = preg_replace("@$mpgurl@", $replacement, $gaz, 1);
					$m++;
				}

			$vidpattern = "%http://(?:\S+\.swf\b|\S+?youtube\S+)%";
			$vidmatch = preg_match_all($vidpattern, $gaz, $vidmatches);
			$g = 0;	
				foreach($vidmatches[0] as $vidurl) {

					$replace =  '%YOUTUBE'.$g.'%';
					$gaz = preg_replace($vidpattern,$replace, $gaz, 1);
					$g++;
				}


			$string = nl2br($gaz);
			$i= 0;

				foreach($matches[0] as $value) {
					$replace = "<img width='500px' height='300px' src='".$value."'></img>";
					$search = "%TOKEN".$i."%";
				    $string = str_replace($search, $replace, $string);
					$i++;
				}

			$e= 0;
				foreach($vidmatches[0] as $vidurl) {	

				$vidurl = substr($vidurl, 31);

					$search = "%YOUTUBE".$e."%";
					$replace = '<embed src="http://www.youtube.com/v/'.$vidurl.'" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350" />';
					$string = str_replace($search, $replace, $string);
					$e++;
				}

			$k= 0;
				foreach($mpgmatches[0] as $mpgurl) {				
					$search = "%MPG".$k."%";
					$replace = '<embed src="'.$mpgurl.'" type="movie/mpg" autoStart="true" width="425" height="350" /></embed>';
					$string = str_replace($search, $replace, $string);
					$e++;
				}

			function bbcode($code)
			{
			$code = preg_replace("/\[code\](.*?)\[\/code\]/","\\1",$code);
			$code = highlight_string($code);
			return $code;
			}

			echo "<p class='ajaxblog'>".bbcode($parser->qParse($string))."<p><br/>";



		}
?>

 

the code above shows how i output my blog onto the page. i use a whole set of preg_matches to search for things such as youtube videos, hyperlinks and images to display on the page.

 

My questions is..... how can i get select everything inide [ code ] [ /code ] and highlight it.#

 

Can someone look at my code and tell me where i am going wrong.

 

many thanks

 

Garry

Is my post invisible?

<?php
function bbcode($code)
{
$code = preg_replace("/\[code\](.*?)\[\/code\]/","\\1",$code);
return $code;
}
$str = bbcode("[code ]<?php echo 'hello'; ?>[/ code]");
echo highlight_string($str);
?>

Without the spaces

Blade i did try and use your function but i must of implemented it wrong

 

i have created a simpler version of the output..

 

Can you tell me if this is correct??

 

<?php
while($row = mysql_fetch_array($result))
		{


		$body = htmlentities($row['body']); 
		$_SESSION['blog_id'] = $row['id'];
		$blog_id = $row['id'];

			include_once "init.inc.php";
			require_once 'HTML/BBCodeParser.php';
			$options = @parse_ini_file('BBCodeParser.ini');
			$parser = new HTML_BBCodeParser($options);			

			echo "<div id='fullblog'>";
			echo "<h1>".$row['title']."</h1>";
			echo "<p class='date'>Posted by ".ucfirst($row['name'])." | ".date("F j, Y",strtotime($row['blog_created']))."</p>";

			$blog_title =  $row['title'];
			$_SESSION['title'] = $blog_title;

			function bbcode($code)
				{
				$code = preg_replace("/\[code\](.*?)\[\/code\]/","\\1",$code);
				return $code;
				}
			$body = bbcode($body);



			echo "<p class='ajaxblog'>".highlight_string($body)."<p><br/>";



		}
?>

Im not quite sure what you mean...

 

if you look at

 

http://www.thedesignmonkeys.co.uk/blog/tokenising_a_string_in_php

 

i have changed teh bg colour to white but look how the post is displayed, it hasnt highlighted anything??

 

whats happening to this??

 

You can get an idea of what is going on if you go to view source and find one of the divs that contain posted code. They use spans to change the colors. Now the process they use to do so is any ones guess and I'd say it is very complex.

A good start I think though would first get the text within " " or ' ' red and then out of all the other functions some how so that the word 'do' in a sting like 'I think i will do that' doesn't get turned green. lol

I'm not much help on the code thing because I still look at "regular statements" with blank stares half the time.

Good luck

to highl;ight the test the parser would be insufficient, you would need to have a function instead thgat out puts the text not a parser wich would re format the text, your new function would be called out(), what it would do is take the message output everything until it gets to a

 take everything from there to 

then output that as highlight_string(the bit) and then the rest of the message would be outputed normaly.

 

substring ereg etc to get the bits of string etc

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.