Displaying code using code heighlighter
I want to insert some programming code along with the descriptions like a
tutorial site.
This is the sample code for HTML code:
$code = "
<p>This is introduction to HTML </p>
[code]
<html>
<head>
<title>This is sample descriptions</title>
</head>
<body>
<form method='post' action='index.php'>
<input type='text' name='username' value=''>
<input type='password' name='password' value=''>
<input type='submit' name='submit' value='Submit'>
</form>
</body>
</html>
[/code]
<p> This is sample for PHP </p>
[code]
<?php
echo "Hi, This is PHP";
?
[/code]
";
$code = mysql_real_escape_string($code);
mysql_query("INSERT INTO tutorials SET tutorial='$code'");
To display iam retrieving from database and using htmlspecialchars like,
echo htmlspecialchars($code);
For heighlighting the codes, I am using, google-code-prettify. It needs,
the code should be in between pre tag with prettyprint class,
<pre class='prettyprint'>
echo htmlspecialchars($code);
</pre>
Whereever the tags, [code] and [/code] are replaced with <pre
class='prettyprint'>"; and </pre> like,
$code = str_replace("[code]", "<pre class='prettyprint'>", $code);
$code = str_replace("[/code]", "</pre>", $code);
When I echo,
echo htmlspecialchars($code);
only plain text is displayed like,
<html> <head> <title>This is sample descriptions</title> </head> <body>
<form method='post' action='index.php'> <input type='text'
name='username' value=''> <input type='password' name='password'
value=''> <input type='submit' name='submit' value='Submit'> </form>
</body> </html> </pre> <h5> 2. This is sample code for paragraph </h5>
<pre class='prettyprint'> <html> <head> <title>This is sample
No comments:
Post a Comment