PDA

View Full Version : BBcode Parsing - Small Problem with [code][/code]



Sylvester21
03-14-2007, 09:05 AM
Hey peepz,

I've got a small problem with the following:



$bbcode = array(
"'\[code\](.*?)\[/code\]'"

$html = array(
"<div class=\"codetop\">CODE</div><div class=\"codemain\"><br /><br />\\1<br /><br /></div></div>"


I've left all other BBcode out and smiley stuff, and other PHP code. I'm using the above (using code example from IPB forums) to replace [*code] with <div class= etc. It works, when the actual [*code] is on one line. If I have more text in the code, it doesn't work, example:



[*code]Blabla here [*/code]

The above works fine. But when it has more text like below:



[*code]Blabla here
And some more blabla here
And then another bla here,
Last but not least, one more bla[*/code]

It doesn't work. Any idea how this can be resolved?

Sylvester21
03-15-2007, 10:35 PM
Nobody can help?

Nightfire
03-15-2007, 10:40 PM
What do you mean doesn't work? Doesn't show the code? Shows the code on one line, even though there's linebreaks in there?

Sylvester21
03-15-2007, 10:49 PM
I'm going to post an image of what I mean:

http://img405.imageshack.us/img405/3157/examplepb9.gif

I've just inserted something into the database, using the bbcode sample parser I posted above. As you can see, it works, but when I posted stuff between the code tags which is more than one line, it doesn't.

Inigoesdr
03-15-2007, 11:07 PM
Try something like this:

<?php
$str = '
blah blah
blahblah


blah';
$bbcode = array('/\[code\](.*)\[\/code\]/s');
$html = array('<div class="codetop">CODE</div><div class="codemain"><br /><br />\\1<br /><br /></div></div>');
echo '<pre>' . preg_replace($bbcode, $html, $str) . '</pre>';
?>

Sylvester21
03-16-2007, 08:02 PM
Nope, this also doesn't work. Isn't there a more easy way of solving this?

Sylvester21
03-25-2007, 02:13 PM
Nobody?

the-dream
03-25-2007, 02:17 PM
dev.bbcode.org

Sylvester21
03-26-2007, 07:58 PM
dev.bbcode.org

Doesn't exist.

marek_mar
03-26-2007, 08:28 PM
Add "s" to the end of the regex.

rafiki
03-26-2007, 08:41 PM
http://www.tutorio.com/tutorial/simple-and-complex-bbcode-with-php/
this gives you a pretty decent idea on how to make your own!



Description: A simple and effective script that allows you to implement bbcode type behaviour on your php website. You can redefine any of the BBCode elements with the built-in CSS Styles.
for this file
http://duncan.xtreme.net.nz/bbCode.php.txt
hope this helps :)

Sylvester21
04-02-2007, 10:24 PM
I believe this is what I needed!

Something like this should work, although I have not tried it out yet. Thanks all for the replies and the help. If it worked I'll post my results here.