我想插入一些编程代码以及教程站点之类的描述。
这是 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 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