Webサイトでの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を使用せずに、またデータベースに挿入する前にコード入力を「フィルタリング」する必要なしに、これを実行することを本当に望んでいます。
ソリューションEmilH に感謝します。
function highlight($t0) {
return preg_replace('/\[code\](.*?)\[\/code\]/ise','"<code>".highlightCode(clean_only(\'$1\'))."</code>"',$t0);
}