Page 1 of 2

BB code: [code]

Posted: Fri Aug 13, 2004 2:24 am
by Lundern
Hi, my portal is in the making.

But I need some help, how can I use preg_replace to call out an highlighting from the code function on this forum, for example:

Code: Select all

<?php echo"I want to have this codething on my site.."; ?>
My code goes like this:

Code: Select all

<?php


$q = @mysql_query("SELECT * FROM art where art_id = '".$_GET&#1111;'art_id']."' and art_type = '".$_GET&#1111;'art_type']."'");

while($r = @mysql_fetch_assoc($q))
&#123;



print("<h3>$r&#1111;art_navn]</h3>
$r&#1111;ingress]

<h3>Artikkelen</h3>

$r&#1111;eksart]");

&#125;


?>
The variable called: $[eksart] is where the article is posted, when it finds for example: [*code] [/*code] I want it to be highlighted just like on this forum..

How do I make this? Does anyone here have a useful code I can use?

Thanks for your help!

Best regard,
Ole

Posted: Fri Aug 13, 2004 2:42 am
by feyd
check on evilwalrus' site.. evilwalrus.com, they've got a few bbcode function scripts there, they aren't fully compliant, but they'll get you started.. or you can try to snag phpbb's bbcode script by downloading their package and loading up bbcode.php from the includes directory..

Posted: Fri Aug 13, 2004 2:47 am
by Lundern
I have already tried that, but the codes they are using is to advanced for me :P

Posted: Fri Aug 13, 2004 2:50 am
by feyd
evilwalrus' are pretty tame, compared to the phpbb ones..

Posted: Fri Aug 13, 2004 3:26 am
by Lundern
Yeah, I looked at some codes, this is the best I have found to now:

Code: Select all

/* Code Format */
	
    $message = eregi_replace('\&#1111;*code](&#1111;^&#1111;]*)\&#1111;/*code]', '<b>Code:</b><blockquote style="width: 100%; margin: 3px 8px 3px 16px; color: #000; background-color: #FFF; border: 2px solid #808080; font-size: 8pt;">\\1</blockquote>', $message);
	
	$php_stuff = eregi('\&#1111;*php](&#1111;^&#1111;]*)\&#1111;/php]', $message, $regs);
	
    $message = eregi_replace('\&#1111;*php](&#1111;^&#1111;]*)\&#1111;/php]', '<b>PHP:</b><br><table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #FAFAFA; border: 1px solid #D1D7DC;">
  <tr>
    <td style="padding-left: 10px; padding-right: 10px; padding-top: 10px; padding-bottom: 10px;">'.highlight_string($regs&#1111;1], true).'</td>
  </tr>
</table>', $message);
	
    /* Code Format */
But it doesnt work :(

EDIT: I cant find the variable I have to change with $r[eksart], which is the mysql variable for the article..

Posted: Fri Aug 13, 2004 3:31 am
by feyd
the basics of it looks like it'd work:

Code: Select all

<?php

$r['eksart'] = preg_replace('#\['.'code](.*?)\
#is', '<b>Code:</b><blockquote style="width: 100%; margin: 3px 8px 3px 16px; color: #000; background-color: #FFF; border: 2px solid #808080; font-size: 8pt;">\\1</blockquote>', $r['eksart']);

?>

Posted: Fri Aug 13, 2004 3:44 am
by Lundern
But that one doesnt highlight, it becomes like this: http://webportalen.lundern.net/?side=in ... e=artikkel

Posted: Fri Aug 13, 2004 3:57 am
by feyd
right.. because it's not run through the php highlighter...

if you want that, you'll need some fancy footwork:

Code: Select all

<?php

$r['eksart'] = preg_replace('#\['.'php](.*?)\['.'/php]#ise',"'<b>PHP:</b><br><table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #FAFAFA; border: 1px solid #D1D7DC;">
  <tr>
    <td style="padding-left: 10px; padding-right: 10px; padding-top: 10px; padding-bottom: 10px;">' . highlight_string('\\1',true) . '</td>
  </tr>
</table>'", $r['eksart']);

?>
that's untested...

Posted: Fri Aug 13, 2004 4:07 am
by Lundern
Nice, it worked great, but (I dont want to spam you down): look at this: http://webportalen.lundern.net/?side=in ... e=artikkel

Why does that come?

PS! Thank you very much for your help, you will get a huge banner/link on the page!

Posted: Fri Aug 13, 2004 4:14 am
by feyd
hmmm might need to add an "addslashes" call to that..not sure..

Posted: Fri Aug 13, 2004 4:16 am
by Lundern
Addslashes didnt work so well as stripslashes did, but then I get all these \ all the time :P

EDIT: + a error message similar to the one I posted..

Posted: Fri Aug 13, 2004 4:38 am
by feyd
I was referring to do something more like:

Code: Select all

<?php

$r['eksart'] = preg_replace('#\['.'php](.*?)\['.'/php]#ise',"'<b>PHP:</b><br><table width="100%" border="0" cellspacing="0" cellpadding="0" style="background-color: #FAFAFA; border: 1px solid #D1D7DC;">
  <tr>
    <td style="padding-left: 10px; padding-right: 10px; padding-top: 10px; padding-bottom: 10px;">' . highlight_string('\\1',true) . '</td>
  </tr>
</table>'", stripslashes($r['eksart'])); 

?>
or putting it inside the evaluation string... could you post your test string as its stored in mysql?

Posted: Fri Aug 13, 2004 4:57 am
by Grim...
If you want to do it the simple way:

Code: Select all

<?php
$posttext = str_replace("[*code]", "<span class="code">", $posttext);
$posttext = str_replace("[/code*]", "</span>", $posttext);
?>

Posted: Fri Aug 13, 2004 5:08 am
by feyd
this is involving php highlighting Grim, it's a tiny bit more complicated.. :D

Posted: Fri Aug 13, 2004 5:18 am
by Lundern
I can upload the files in .phps, so that you can look at the files.

This is were the "selective highlight" comes in the picture: http://webportalen.lundern.net/feyd/innlegg.phps

This is the action file that put all the info into the database: http://webportalen.lundern.net/feyd/add_art_act.phps

This is what is in the table (in mysql):
[*php]
<?php

echo"hello world!";

?>
EDIT: Url to the "article": http://webportalen.lundern.net/?side=in ... e=artikkel