突出显示数据库中的代码

发布于 2024-08-05 01:50:33 字数 2123 浏览 6 评论 0原文

我在网站上显示 PHP 代码(存储在数据库中)时遇到问题。

这是我在数据库中的文本:

Blah Blah Blah this is regular text    
[code]
<?php
$message = \"<div class=\'code\' style=\\\"NO\\\">\";
echo $message;
?>
[/code]
Blah Blah Blah this is more regular text

我想显示为:

Blah Blah Blah this isregular text
<?php
$message = "<div class='code' style=\"NO\">";
echo $message
?>
Blah Blah Blah this is more regular text

现在我所做的如下:

<?php

echo nl2br(highlight(clean_only($post['post'])));

function clean_only($input) {
    if(get_magic_quotes_gpc()) {
        $return = stripslashes($input);
    }
    else {
        $return = $input;
    }
    return $return;
}

function highlight($t0) {
    return preg_replace('/\[code\](.*?)\[\/code\]/ise',"'<code>'.highlightCode(\"$1\").'</code>'",$t0);
}

function highlightCode($code) {
    $source_code = highlight_string($code,true);
    $source_code = str_replace(
        array('style="color: #0000BB"','style="color: #007700"','style="color: #DD0000"','style="color: #FF8000"','style="color: #000000"'),
        array('class="phpdefault"','class="phpkeyword"','class="phpstring"','class="phpcomment"','class="htmldefault"'),
        $source_code);
    return "<div class='code'><table cellspacing='1' cellpadding='2'><tr><th valign='top'>".implode("<br />",range(1,substr_count($source_code,"<br />")-1))."</th><td valign='top' nowrap='nowrap'>".str_replace("<code><span class=\"htmldefault\"><br />","<code><span class=\"htmldefault\">",preg_replace("/[\n\r]/","",$source_code))."</td></tr></table></div>";
}
?>

由于某种原因,它删除了所有 PHP 变量,并且还弄乱了引号和反斜杠。显然,有些反斜杠需要保留在那里,有些则需要删除。

仅供参考 - 我真的希望在不使用 JavaScript 的情况下完成此操作,并且无需在将代码输入插入数据库之前“过滤”它。

解决方案 特别感谢 Emil H:

function highlight($t0) {
    return preg_replace('/\[code\](.*?)\[\/code\]/ise','"<code>".highlightCode(clean_only(\'$1\'))."</code>"',$t0);
}

I have a problem regarding display PHP code (stored in a database) on a website.

This is the text I have in the database:

Blah Blah Blah this is regular text    
[code]
<?php
$message = \"<div class=\'code\' style=\\\"NO\\\">\";
echo $message;
?>
[/code]
Blah Blah Blah this is more regular text

Which I want to display as:

Blah Blah Blah this isregular text
<?php
$message = "<div class='code' style=\"NO\">";
echo $message
?>
Blah Blah Blah this is more regular text

Now what I have done is the following:

<?php

echo nl2br(highlight(clean_only($post['post'])));

function clean_only($input) {
    if(get_magic_quotes_gpc()) {
        $return = stripslashes($input);
    }
    else {
        $return = $input;
    }
    return $return;
}

function highlight($t0) {
    return preg_replace('/\[code\](.*?)\[\/code\]/ise',"'<code>'.highlightCode(\"$1\").'</code>'",$t0);
}

function highlightCode($code) {
    $source_code = highlight_string($code,true);
    $source_code = str_replace(
        array('style="color: #0000BB"','style="color: #007700"','style="color: #DD0000"','style="color: #FF8000"','style="color: #000000"'),
        array('class="phpdefault"','class="phpkeyword"','class="phpstring"','class="phpcomment"','class="htmldefault"'),
        $source_code);
    return "<div class='code'><table cellspacing='1' cellpadding='2'><tr><th valign='top'>".implode("<br />",range(1,substr_count($source_code,"<br />")-1))."</th><td valign='top' nowrap='nowrap'>".str_replace("<code><span class=\"htmldefault\"><br />","<code><span class=\"htmldefault\">",preg_replace("/[\n\r]/","",$source_code))."</td></tr></table></div>";
}
?>

For some reason, it removed all PHP variables, and also messes up the quotes and backslashes. Obviously there are some backslashes that need to stay there, and some that need to go.

FYI - I'm really looking to do this without the use of JavaScript, and without have to "filter" my code input before inserting it into the database.

SOLUTION
Special thanks to Emil H:

function highlight($t0) {
    return preg_replace('/\[code\](.*?)\[\/code\]/ise','"<code>".highlightCode(clean_only(\'$1\'))."</code>"',$t0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2024-08-12 01:50:33

当 preg_replace 执行第二个参数中的代码时,问题可能会突出显示。由于美元符号未转义,PHP 将执行常规字符串插值。尝试一下:

function highlight($t0) {
    return preg_replace('/\[code\](.*?)\[\/code\]/ise','"<code>".highlightCode(\'$1\')."</code>"',$t0);
}

区别在于第二个参数。你有:

"'<code>'.highlightCode(\"$1\").'</code>'"

它必须是:

'"<code>".highlightCode(\'$1\')."</code>"'

请注意,我已经颠倒了“”和“”的用法。

The problem is probably in highlight when preg_replace executes the code in the second parameter. Since the dollar signs are unescaped PHP will perform a regular string interpolation. Try:

function highlight($t0) {
    return preg_replace('/\[code\](.*?)\[\/code\]/ise','"<code>".highlightCode(\'$1\')."</code>"',$t0);
}

The difference is in the second parameter. You have:

"'<code>'.highlightCode(\"$1\").'</code>'"

It has to be:

'"<code>".highlightCode(\'$1\')."</code>"'

Note that I've inversed the usage of '' and "".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文