bbcode
Started by Stu, Jan 06 2006 06:05 PM
7 replies to this topic
#1
Posted 06 January 2006 - 06:05 PM
can someone help me out or point me in the right direction with this please...
im not in need of anything like [b] or [i] or [url] etc.
im needing something more like the [code] tag like on this board, i cant seem to find anything on it. so basically i need something that will stop the parsing of any html or php or js inside the [code] blocks...just like on the forums...
i had a look at a pear tutorial somewhere, BBcode_htmlparser or something, but im not sure if thats the right direction i should be heading, it all seemed like gibberish to me, on top of that it wasnt really well explained... but anyways, has anyone got any ideas?
im not in need of anything like [b] or [i] or [url] etc.
im needing something more like the [code] tag like on this board, i cant seem to find anything on it. so basically i need something that will stop the parsing of any html or php or js inside the [code] blocks...just like on the forums...
i had a look at a pear tutorial somewhere, BBcode_htmlparser or something, but im not sure if thats the right direction i should be heading, it all seemed like gibberish to me, on top of that it wasnt really well explained... but anyways, has anyone got any ideas?
#2
Posted 06 January 2006 - 06:47 PM
Found this out by looking at phpBB's source a long while back. It's really no different then the traditional "fill an array" method that you see everywhere, but you have to add a few extra steps.
<?
$code = preg_match_all('#\[code\](.*?)\[/code\]#si', $post, $match);
foreach($match[1] as $key=>$string){
$string = trim($string);
$parsed[] = htmlspecialchars($string);
}
for($x=0; $x < count($parsed[0]); $x++){
$text = str_replace($match[0][$x], '<span name="code">'.$parsed[$x].'</span>', $text);
}
?>
note: I may have added a few more steps then you want, but it gives you the basic idea. This is also untested, so you will most likely have to kick it a few times to make it work #3
Posted 06 January 2006 - 07:48 PM
mate you are an absolute diamond. ill try it out and report back asap 
EDIT: ok i have a bit of a problem. i dont actually want to use it as bbcode as such, because i want to edit an html page directly, and somehow stop any html or php from being parsed - a bit like how [code] works... would i use the same code you have posted but just play around with it or what? anymore pointers would be appreciated
sorry i didnt really explain properly the first time
EDIT: ok i have a bit of a problem. i dont actually want to use it as bbcode as such, because i want to edit an html page directly, and somehow stop any html or php from being parsed - a bit like how [code] works... would i use the same code you have posted but just play around with it or what? anymore pointers would be appreciated
sorry i didnt really explain properly the first time
Edited by Stu, 06 January 2006 - 08:02 PM.
#4
Posted 06 January 2006 - 09:11 PM
htmlspecialchars($string);
If what i'm thinking is right, that should be all you need out of that code.
If what i'm thinking is right, that should be all you need out of that code.
#5
Posted 06 January 2006 - 09:38 PM
ok that did the trick, thanks. but the problem i have now, is that its not preserving white space and line breaks... ive tried replacing the spaces and tabs and line breaks with this:
but as its using htmlspecialchars, it just convert them to html and shows it in as and <br> instead of spaces and line breaks...
i know i might seem a bit stupid here but anymore ideas?
EDIT:
works a charm, thanks for the help
anymore suggestions on how i could clean it up would be welcome though?
function code($code){
$code = str_replace(" ", " ", $code);
$code = str_replace(" ", " ", $code);
$code = str_replace("\t", " ", $code);
$code = str_replace("\n", "<br />", $code);
$parsed = htmlspecialchars($code);
echo '<div class="code">';
echo $parsed;
echo '</div>';
}
but as its using htmlspecialchars, it just convert them to html and shows it in as and <br> instead of spaces and line breaks...
i know i might seem a bit stupid here but anymore ideas?
EDIT:
function code($code){
$parsed = htmlspecialchars($code);
$parsed = str_replace(" ", " ", $parsed);
$parsed = str_replace(" ", " ", $parsed);
$parsed = str_replace("\t", " ", $parsed);
$parsed = str_replace("\n", "<br />", $parsed);
echo '<div class="code"><h3>Code:</h3>';
echo $parsed;
echo '</div>';
}
works a charm, thanks for the help
Edited by Stu, 06 January 2006 - 10:29 PM.
#6
Posted 07 January 2006 - 02:04 PM
Well, if you're looking to just output the php (for like a tutorial or something), you could look into highlight_string()
http://php.net/highlight_string
http://deceptive-log...l/php/highlight <- my info about how to use it (a little more in-depth then php.net's)... It will basically do everything in your function, and color php code so it's a little more distinguishable.
But if that's not what you want, then you could change your function to this:
http://php.net/highlight_string
http://deceptive-log...l/php/highlight <- my info about how to use it (a little more in-depth then php.net's)... It will basically do everything in your function, and color php code so it's a little more distinguishable.
But if that's not what you want, then you could change your function to this:
function code($code){
echo '<div class="code" style="white-space:pre;">'.htmlspecialchars($code).'</div>';
}
Edited by rc69, 07 January 2006 - 02:06 PM.
#7
Posted 07 January 2006 - 04:50 PM
thanks for that, ill look into the highlight strings, ive actually read your tutorial before and considered it but never got round to it (till now).
i take it the second piece of code is basically and easier version of what i did? does style="white-space:pre; preserve the whitespace instead of using all the str_replace like i had done??
i take it the second piece of code is basically and easier version of what i did? does style="white-space:pre; preserve the whitespace instead of using all the str_replace like i had done??
#8
Posted 07 January 2006 - 07:29 PM
yep, just as if you were using a <pre> tag.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
