http://www.fixdown.com/article/article/1841.htm

解决方案 »

  1.   

    <script language="javascript">
    var str,str2,str3;
    str="sdfsdfasfsf[code]我是中国人[/code]sdfss";
    re=/(\[code\])(.*)(\[\/code\])/g ;
    str2=str.replace(re,"<FONT color=#d56000>"+"$2"+"</FONT>");
    document.write(str2)
    </script>
    不过上面的只能解决带有一对[code]的。多对就会取最两边的了。。
      

  2.   

    <?php
    $s = <<< TEXT
    string0
    string1
    [code]
    string2
    [/code]string3
    string4
    TEXT;echo preg_replace("/\[code\](.+)\[\/code\]/sU","<font color=red>\\1</font>",$s);
    ?>
      

  3.   

    (唠叨) 大哥解决了提取的目的,但没有实现将其高亮显示,我说的高亮指的是形如highlight_string的效果,请问能否实现?
      

  4.   

    还有,如果我的string2是一段HTML代码或PHP代码,上面的将不会达到目的,比如将string2替换成<? echo "hello world";?>
      

  5.   

    preg_replace+e 或者 preg_replace_callback一个例子
    <?php
    $s = <<< TEXT
    string0
    string1
    [code]
    string2  <?php echo "hello"; ?>
    [/code]string3
    string4
    TEXT;
    function foo($m)
    {
        return highlight_string(stripslashes($m));
    }
    echo preg_replace("/\[code\](.+)\[\/code\]/sUe","foo('\\1')",$s);
    ?>
      

  6.   

    to ccterran(iwind)
    你似乎解决了高亮显示的效果,但显示的位置不正确
    这样做的话,就将[code][/code]之间的内容置首显示了
      

  7.   

    $str=[code]some code[/code]//highlight code
    preg_match_all("/\[code\](.*)\[\/code\]/iUs","$code_str",$c_match,PREG_PATTERN_ORDER);
    $array_num=count($c_match[0]);
    for ($i=0;$i<$array_num;$i++)
    {
    $match_all=$c_match[0][$i];
    $h_str=$c_match[1][$i];
    $h_str=highlight_string($h_str,"ture");
    $h_str="Code:<br />=======begin=======<br />".$h_str."<br />========end=======<br />";
    $code_str=str_replace($match_all,$h_str,$code_str);
    }
    echo $code_str;
      

  8.   

    test:
    http://surfchen.wegame.com/show_post.php?fid=15&pid=f_360