php – 使用代码荧光笔显示代码
发布时间:2020-12-13 22:49:46 所属栏目:PHP教程 来源:网络整理
导读:我想插入一些编程代码以及教程站点之类的描述. 这是HTML代码的示例代码: $code = " pThis is introduction to HTML /p [code]html head titleThis is sample descriptions/title/headbody form method='post' action='index.php' input type='text' name='u
我想插入一些编程代码以及教程站点之类的描述.
这是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 解决方法
您在进行替换后调用了htmlspecialchars,因此< pre>标签也被转义(并且不会呈现为HTML).颠倒顺序应该可以解决问题:
$code = htmlspecialchars($code); $code = str_replace("[code]",$code); $code = str_replace("[/code]",$code); echo $code; 此外,请查看highlight_string以突出显示源代码. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |