Welcome Guest, Not a member yet? Register   Sign In
showing code
#1

[eluser]St0neyx[/eluser]
Hi all,

I'm making an Code Igniter help site for my work, i'm the first who's working/testing codeigniter and want to make some code examples and stuff..
(also makes me more experienced with CI)
now is my question..
how do i print out code like this
(and i mean what you see the php code with colors and all)
Code:
<?php echo 'example code'; ?>
Is there an helper/librarie/class/plugin or something for this?
#2

[eluser]batteries[/eluser]
have you checked the manual? it should have something in there.
#3

[eluser]gunter[/eluser]
yes, I think there was something...
maybe try:
highlight_string(String);
highlight_file (File);

ahhh!
search in the user_guide for: highlight_code()
#4

[eluser]batteries[/eluser]
i was trying to teach a man how to fish.. and you ruined it by giving it to him Sad
#5

[eluser]esra[/eluser]
You might also look as the Geshi library. (Do a web search).
#6

[eluser]St0neyx[/eluser]
[quote author="batteries" date="1185242797"]have you checked the manual? it should have something in there.[/quote]
[quote author="batteries" date="1185244941"]i was trying to teach a man how to fish.. and you ruined it by giving it to him Sad[/quote]

I did look at the forums and userguide.. ;-)
I always do that, but could'n find it.. thats why i posted it..
#7

[eluser]St0neyx[/eluser]
[quote author="gunter" date="1185244684"]yes, I think there was something...
maybe try:
highlight_string(String);
highlight_file (File);

ahhh!
search in the user_guide for: highlight_code()[/quote]

Thx i will look at it

Edit:

Hmm it works half..

if i do this
Code:
$string =
  "
function index()
{
// definde the page array
$page = array();
$data = array();

// define the header content and footer

// load the page
$this->load->view('index', $page);
}
  ";

and if i print out the code this is the result

function index()
{
// definde the page array
Array = array();
Array = array();

// define the header content and footer

// load the page
Object id #10->view('index', Array);
}

you can see my page and data arrays are different and the this->load->view is different
#8

[eluser]pr0digy[/eluser]
You can also use JavaScript for this. Here is a nice little library - code prettify. I use it on my site.
#9

[eluser]glemigh[/eluser]
I found this somewhere, and hacked it up for my needs, maybe you can get some ideas from it.

source.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;
&lt;head&gt;&lt;title&gt;&lt;/title&gt;
&lt;style type="text/css" media="all"&gt;
  @import url("source.css");
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;

&lt;?php

$_SERVER['BASE_PAGE'] = 'source.php';

// Use class names instead of colors
ini_set('highlight.comment', 'comment');
ini_set('highlight.default', 'default');
ini_set('highlight.keyword', 'keyword');
ini_set('highlight.string',  'string');
ini_set('highlight.html', 'html');

// Highlight PHP code
function highlight_php($code, $return = FALSE)
{
// Using OB, as highlight_string() only supports
// returning the result from 4.2.0
ob_start();
highlight_string($code);
$highlighted = ob_get_contents();
ob_end_clean();

// This should eventually be a php_syntax_check() call when we move to PHP5
// But use this ugly hack for now to avoid code snippets with bad syntax screwing up the highlighter  
if(strstr($highlighted,"include/layout.inc</b>")) $highlighted = '<span class="default">'.htmlentities($code)."</span>";
  
// Fix output to use CSS classes and wrap well
$highlighted = '<div class="phpcode">' . str_replace(
  array(
   '&nbsp;',
   '<br />',
   '<font color="',  // for PHP 4
   '<span style="color: ', // from PHP 5.0.0RC1
   '</font>',
   "\n ",
   '  '
  ),
  array(
   ' ',
   "<br />\n",
   '<span class="',
   '<span class="',
   '</span>',
   "\n&nbsp;",
   '&nbsp; '
  ),
  $highlighted
) . '</div>';

if ($return) { return $highlighted; }
else { echo $highlighted; }
}



// No file param specified
if (!isset($_GET['url']) || (isset($_GET['url']) && !is_string($_GET['url']))) {
echo "<h1>No page URL specified</h1>";
echo "Try something like: http://janus.georgelemigh.pvt/source.php?url=/source.php";
// site_footer();
exit;
}

echo "<h1>Source of: " . htmlentities($_GET['url']) . "</h1>";

// Get dirname of the specified URL part
$dir = dirname($_GET['url']);

$page_name = $_SERVER['DOCUMENT_ROOT'] . $_GET['url'];
// Provide some feedback based on the file found
if (!$page_name || @is_dir($page_name)) {
echo "<p>Invalid file or folder specified<br>" . $page_name . "</p>\n";
} elseif (file_exists($page_name)) {
highlight_php(join("", file($page_name)));
} else {
echo "<p>This file does not exist.<br>" . $page_name . "</p>\n";
}
?&gt;
&lt;/body&gt;
&lt;/html&gt;
#10

[eluser]glemigh[/eluser]
and the css, source.css is to big to paste into a message, so I tar'ed up both and you can grab it here:

source_show.tgz

Hope it helps you in some way torwards your goal.

George




Theme © iAndrew 2016 - Forum software by © MyBB