Page 1 of 2

Tutorial Script

Posted: Sat May 22, 2004 1:46 pm
by John Cartwright
I am having a brain bubble and can't figure out this design.. I have a bunch of code stored in a database and I want to guide the users step by step through tutorails that I will write.

so it will be:

text
code

text
code

etc..

Everythign will be stored in a database, and I'm wondering if anyone has any advice for me..

Also, this will be the function I use for parsing the code..

Code: Select all

<?php
function phpHighlight($code)
	{
	$code = "<?php\n".$code."\n?>";
    $code = stripslashes($code);
    $code = highlight_string($code, true);
    $code = explode('<br />', $code);
   	
    //put a table here and echo the code
}
?>

Posted: Sat May 22, 2004 2:10 pm
by d3ad1ysp0rk
I was thinking you could use 2 tables (one for code, one for plain text), but then I couldn't think of a good (and efficient) way to tell the script which code/text goes with which tutorial, and in what order the chunks go (ie. text1, code1, text2, code2, etc).

You might be better just keeping the code AND text in a blog/text field, and maybe splitting it up by pages before hand.

table tutorials:
tutID | content | page_num

tutorials.php?t=4&p=2

Code: Select all

if(empty($_GET['p'])){ $p = 1; }
else { $p = $_GET['p']; }
$result = mysql_query("SELECT content FROM tutorials WHERE tutID = '" .$_GET['t']. "' AND page_num = '$p' LIMIT 1";
$content = mysql_result($result);
$content = phpHighlight($content);
$page = include("template.inc");
$page = str_replace("{content}",$content,$page);
Hope that helps.

Posted: Sat May 22, 2004 3:31 pm
by John Cartwright
Thats a better answer than I expected.. you've been lots of help TY :)

Posted: Sat May 22, 2004 5:56 pm
by John Cartwright
How can I detect <? and ?> in the code and put tables around the whole code?

Posted: Sat May 22, 2004 7:36 pm
by McGruff
Use regex to find php code, then apply a php highlighter fn to the matches. For example this will find anything enclosed in [ php ] ..[ /php ] tags:

"#\[php\](.*?)\[/php\]#si"

Posted: Sun May 23, 2004 12:49 am
by John Cartwright
TY alot mcgruff, I'll try it out first thing tommorow and let ya know the results.

*edit* I have never used the highlighting but do I have to edit php.ini to get the same colouring as this forum.. or is that default ?

Posted: Sun May 23, 2004 12:53 am
by d3ad1ysp0rk

Posted: Sun May 23, 2004 12:55 am
by John Cartwright
k coo ty

Posted: Thu Jun 17, 2004 11:47 am
by John Cartwright
K I finally got started on this part of my site... and I'm having some problems with the highlighting color
; Colors for Syntax Highlighting mode. Anything that's acceptable in
<font color="??????"> would work.
highlight.string = #DD0000
highlight.comment = #FF9900
highlight.keyword = #007700
highlight.bg = #FFFFFF
highlight.default = #0000BB
highlight.html = #000000
This is tutorial.php so far

Code: Select all

<?php

$tutorials = new tutorials;

/* NOT USED FOR DEBUGGING

mysql_connect("localhost","root","");
mysql_select_db("main");
$result = @mysql_query("SELECT * FROM tutorials_php") or die('Error performing query: '. mysql_error());	
$row = mysql_fetch_array($result);
$code = $row["code"]; */
	
$code = "<? echo "hello World" ?>";
		$tutorials->phphighlight($code); 
		
?>
This is the function

Code: Select all

<?php
class tutorials {

				function phphighlight($code)
					{
				/*	$code = "<?php\n".$code."\n?>"; */
    				$code = stripslashes($code);
    				$code = highlight_string($code, true);
    				//$code = explode('<br />', $code);
   	
					echo $code;
					}
				}
?>

and it outputs every code I throw at it in highlight.keyword = #007700

Any suggestions on what I'm doing wrong?

Posted: Thu Jun 17, 2004 11:48 am
by d3ad1ysp0rk
$tutorials = new tutorials();

Posted: Thu Jun 17, 2004 11:49 am
by John Cartwright
Oh ya forgot..but back to the issue at hand

Posted: Thu Jun 17, 2004 12:19 pm
by John Cartwright
doing a search AGAIN on the forums I came across this thread

viewtopic.php?t=22400&highlight=highlight

and came up with this..

Code: Select all

<?php
  $allLines = 'this is a test of the div tagging... [ php]<?php $variable = "check this out ma!"; ?>[ /php] joy joy joy [ php]<?php $variable 
    
    
    = 
    
     "check this out ma!"; ?>[ /php] some more useless text'; 

   $output = preg_replace('|\[ php](.*?)\[/ php]|ise','highlight_string("\\1",true)',$allLines); 

    
   echo $output; ?>
which is exackly I want....

but I sitll can't get it to highlight properly.. :(

Posted: Thu Jun 17, 2004 12:33 pm
by markl999
The below works ok for me:

<?php
$allLines = '
code starts below
[syntax=php]<?php
$variable = \'hello\';
?>[/syntax]
and some more code
[syntax=php]<?php
$foo = \'eek\';
echo $foo;
?>[/syntax]
';
$output = preg_replace('|\[syntax=php](.*?)\[/syntax]|ise',"str_replace('<br />', '', highlight_string('\\1',true))", $allLines);
echo nl2br($output);
?>

(I didn't use bbcode php tags in the post as it mucks up the tags in the example)

Posted: Thu Jun 17, 2004 12:35 pm
by feyd
this works on my site/system:

Code: Select all

$message = preg_replace("/\&#1111;php](.*?)\&#1111;\/php]/ise", "'<b>PHP:</b><blockquote style="width: 100%; margin: 3px 8px 3px 16px; color: #000; background-color: #FFF; border: 2px solid #808080; font-size: 8pt;">'.highlight_string('\\1',true).'</blockquote>'", $message);
I'm still fiddling with it though..

Posted: Thu Jun 17, 2004 1:30 pm
by John Cartwright
Wow thats great.. both of you..

I still have 1 problem tho

PHP syntax highlihging is all coming out 007777 or whatever.. ( read a few posts above ) and