BB code: [code]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Lundern
Forum Newbie
Posts: 8
Joined: Fri Aug 13, 2004 2:24 am

BB code: [code]

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
Lundern
Forum Newbie
Posts: 8
Joined: Fri Aug 13, 2004 2:24 am

Post by Lundern »

I have already tried that, but the codes they are using is to advanced for me :P
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

evilwalrus' are pretty tame, compared to the phpbb ones..
Lundern
Forum Newbie
Posts: 8
Joined: Fri Aug 13, 2004 2:24 am

Post 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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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']);

?>
Lundern
Forum Newbie
Posts: 8
Joined: Fri Aug 13, 2004 2:24 am

Post by Lundern »

But that one doesnt highlight, it becomes like this: http://webportalen.lundern.net/?side=in ... e=artikkel
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
Lundern
Forum Newbie
Posts: 8
Joined: Fri Aug 13, 2004 2:24 am

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

hmmm might need to add an "addslashes" call to that..not sure..
Lundern
Forum Newbie
Posts: 8
Joined: Fri Aug 13, 2004 2:24 am

Post 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..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

this is involving php highlighting Grim, it's a tiny bit more complicated.. :D
Lundern
Forum Newbie
Posts: 8
Joined: Fri Aug 13, 2004 2:24 am

Post 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
Post Reply