gaza165 Posted October 27, 2008 Share Posted October 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/ Share on other sites More sharing options...
MasterACE14 Posted October 27, 2008 Share Posted October 27, 2008 you can look into highlight_string(); for coloring the PHP code. and look at preg_match(); for the [ code ] tags. Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675469 Share on other sites More sharing options...
gaza165 Posted October 27, 2008 Author Share Posted October 27, 2008 How do i get it to distinguish the different parts of php?? Like how do i recognise if its a function or a variable or a string or a number etc?? <?php $string = str_replace($string,"DFfsd","FDSfdsf"); echo $string ?> Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675472 Share on other sites More sharing options...
DeanWhitehouse Posted October 27, 2008 Share Posted October 27, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675518 Share on other sites More sharing options...
MasterACE14 Posted October 27, 2008 Share Posted October 27, 2008 use preg_match(); look it up on php.net and take a look at the Regular Expressions tutorial on phpfreaks.com Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675521 Share on other sites More sharing options...
gaza165 Posted October 27, 2008 Author Share Posted October 27, 2008 Can you guys tell me how i would get the string between two values such as below [ code ]<? echo "hello"; ?>[/ code] that will bring back <? echo "hello"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675531 Share on other sites More sharing options...
DeanWhitehouse Posted October 27, 2008 Share Posted October 27, 2008 <?php function bbcode($code) { $code = preg_replace("/\[code\](.*?)\[\/code\]/","\\1",$code); $code = highlight_string($code); return $code; } echo bbcode("<?php echo 'hello';?>"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675534 Share on other sites More sharing options...
gaza165 Posted October 27, 2008 Author Share Posted October 27, 2008 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675614 Share on other sites More sharing options...
DeanWhitehouse Posted October 27, 2008 Share Posted October 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675621 Share on other sites More sharing options...
gaza165 Posted October 27, 2008 Author Share Posted October 27, 2008 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/>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675628 Share on other sites More sharing options...
DeanWhitehouse Posted October 27, 2008 Share Posted October 27, 2008 Looks correct, does it not work? Also if it doesn't add error_reporting(E_ALL); to the top of the page Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675630 Share on other sites More sharing options...
gaza165 Posted October 27, 2008 Author Share Posted October 27, 2008 http://www.thedesignmonkeys.co.uk/blog/tokenising_a_string_in_php if you look at that and highlight the page as the text is absent from view for some reason.. this is what it gives me.... not sure why?? Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675633 Share on other sites More sharing options...
DeanWhitehouse Posted October 27, 2008 Share Posted October 27, 2008 When you use the highlight_string function it highlights php code, and parses everything else black, so that's why the text is not visible without selecting it. Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675635 Share on other sites More sharing options...
gaza165 Posted October 27, 2008 Author Share Posted October 27, 2008 Ok, so what do i need to do in order for this to work?? Thanks man this has helped me alot Garry Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675643 Share on other sites More sharing options...
DeanWhitehouse Posted October 27, 2008 Share Posted October 27, 2008 Give the page a different colour ? Or maybe give the text a light background color? Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675645 Share on other sites More sharing options...
gaza165 Posted October 27, 2008 Author Share Posted October 27, 2008 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?? Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675648 Share on other sites More sharing options...
DeanWhitehouse Posted October 27, 2008 Share Posted October 27, 2008 Are you using highlight on everything? As i think it works like htmlentities does and produces the ampersand for the tag. Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675651 Share on other sites More sharing options...
gaza165 Posted October 27, 2008 Author Share Posted October 27, 2008 I just want to highlight the text inside the [ code ] tags?? Is it htmlentities that is messing it up? Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675657 Share on other sites More sharing options...
nadeemshafi9 Posted October 27, 2008 Share Posted October 27, 2008 i would take a parser approach, insert everything into the DB as is inputed by the user, but pars it when outputed into the site, so echo message would be echo parse($message); Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675661 Share on other sites More sharing options...
Brian W Posted October 27, 2008 Share Posted October 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675664 Share on other sites More sharing options...
DeanWhitehouse Posted October 27, 2008 Share Posted October 27, 2008 I am having the same problem with it. I do not know why it isn't working, but i'm sure one of the experts(hint:DarkWater ) will come and solve it Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675665 Share on other sites More sharing options...
nadeemshafi9 Posted October 27, 2008 Share Posted October 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675667 Share on other sites More sharing options...
nadeemshafi9 Posted October 27, 2008 Share Posted October 27, 2008 i was asked to write a script to support markup within markup that was hard Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675669 Share on other sites More sharing options...
gaza165 Posted October 27, 2008 Author Share Posted October 27, 2008 this is a toughy guys. there must be a way to select just the text inside the [ code ] tags and highlight the php code. anyone wanna fire any solutions my way. Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675670 Share on other sites More sharing options...
DeanWhitehouse Posted October 27, 2008 Share Posted October 27, 2008 Use arrays? Explode at the tags? Quote Link to comment https://forums.phpfreaks.com/topic/130247-help-to-create-a-forum-bbcodes/#findComment-675675 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.