Javascript 在博客中准备和显示代码示例的替代方法

Javascript 在博客中准备和显示代码示例的替代方法,javascript,php,jquery,tinymce,Javascript,Php,Jquery,Tinymce,我在写一个小博客,用TinyMCE和PHP/MySql来处理/存储数据。我以前做过几次,但在这一次中,我想用”)编写示例代码块; $("#change").click(function(){ var output = $("#source").val().replace(/</g,"&lt;"); output = output.replace(/>/g,"&gt;"); $("#output").val("<pre>"+output+"<

我在写一个小博客,用TinyMCE和PHP/MySql来处理/存储数据。我以前做过几次,但在这一次中,我想用
”)编写示例代码块;
$("#change").click(function(){
  var output = $("#source").val().replace(/</g,"&lt;");
  output = output.replace(/>/g,"&gt;");
  $("#output").val("<pre>"+output+"</pre>");
});
});
一次在数据库中插入字符串是有效的,但我发现当我想更新它时,在文本区域中显示的实体再次发生了变化。因此,要更新到数据库,我需要再次替换字符串的字符

因此,我编写了这个小函数来准备要用PHP插入db的字符串:

函数processCode($string\u raw,$start,$end){
if(preg_match_all(“/”$start.(.*?).str_replace(“/”、“\/”、$end)。“/s”、$string_raw,$matches)){

对于($i=0;$iTinyMCE,它有一个
codesample
插件,专门设计用于允许用户将代码示例插入其内容:

如果您使用它,您不需要所有的代码来处理粘贴并将其包装在
标记中

function processCode($string_raw,$start,$end) {
    if(preg_match_all("/".$start."(.*?)".str_replace("/","\/",$end)."/s",$string_raw,$matches)) {

        for($i=0 ; $i<count($matches[0]) ; $i++) {
            $processed = htmlentities($matches[1][$i]);
            $string_raw = str_replace(  $matches[0][$i], 
                                        $start.$processed.$end, 
                                        $string_raw);
        }
    }
    return $string_raw;
}
$raw_string = "This code <pre><b>code</b></pre> is mine";