我希望能在教程网站中插入一些编程代码,并附上描述。
以下是HTML代码示例:
$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'");
为了展示,我正在从数据库中检索内容,并使用
htmlspecialchars
,如下所示: echo htmlspecialchars($code);
为了突出显示代码,我使用
google-code-prettify
,它要求将代码置于pre
标签内,并添加prettyprint
类。 <pre class='prettyprint'>
echo htmlspecialchars($code);
</pre>
无论标记出现在何处,将[code]
和[/code]
替换为<pre class='prettyprint'>
和</pre>
,如下:
$code = str_replace("[code]", "<pre class='prettyprint'>", $code);
$code = str_replace("[/code]", "</pre>", $code);
当我使用echo命令时,
echo htmlspecialchars($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> </pre> <h5> 2. This is sample code for paragraph </h5> <pre class='prettyprint'> <html> <head> <title>This is sample