我想插入一些编程代码与描述一样,一个教程网站。
以下是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,它要求代码位于带有prettyprint类的pre标记之间。
<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发布于 2014-08-24 05:46:23
您在之后调用htmlspecialchars 进行替换,这样<pre>标记也会被转义(并且不会呈现为HTML)。颠倒顺序应该可以做到这一点:
$code = htmlspecialchars($code);
$code = str_replace("[code]", "<pre class='prettyprint'>", $code);
$code = str_replace("[/code]", "</pre>", $code);
echo $code;此外,还可以查看用于突出显示源代码的highlight_string。
发布于 2013-08-23 22:03:22
在您的代码中也有<pre class='prettyprint'>和</pre>语句。这可能会使代码无法按预期工作。
您可以尝试将代码中的<和>分别更改为<和>。
https://stackoverflow.com/questions/18404476
复制相似问题