wdphp基础函数加入代码高亮功能 wdphp加入分页类和上传类使得框架更好的得到完善
/** * 代码加亮 * @param String $str 要高亮显示的字符串 或者 文件名 * @param Boolean $show 是否输出 * @return String */ function highlight_code($str,$show=false) { if(file_exists($str)) { $str = file_get_contents($str); } $str = stripslashes(trim($str)); // The highlight string function encodes and highlights // brackets so we need them to start raw $str = str_replace(array('<', '>'), array('<', '>'), $str); // Replace any existing PHP tags to temporary markers so they don't accidentally // break the string out of PHP, and thus, thwart the highlighting. $str = str_replace(array('<?php', '?>', '\\'), array('phptagopen', 'phptagclose', 'backslashtmp'), $str); // The highlight_string function requires that the text be surrounded // by PHP tags. Since we don't know if A) the submitted text has PHP tags, // or B) whether the PHP tags enclose the entire string, we will add our // own PHP tags around the string along with some markers to make replacement easier late $str = '<?php //tempstart'."\n".$str.'//tempend ?>'; // <? // All the magic happens here, baby! $str = highlight_string($str, TRUE); // Prior to PHP 5, the highlight function used icky font tags // so we'll replace them with span tags. if (abs(phpversion()) < 5) { $str = str_replace(array('<font ', '</font>'), array('<span ', '</span>'), $str); $str = preg_replace('#color="(.*?)"#', 'style="color: \\1"', $str); } // Remove our artificially added PHP $str = preg_replace("#\<code\>.+?//tempstart\<br />\</span\>#is", "<code>\n", $str); $str = preg_replace("#\<code\>.+?//tempstart\<br />#is", "<code>\n", $str); $str = preg_replace("#//tempend.+#is", "</span>\n</code>", $str); // Replace our markers back to PHP tags. $str = str_replace(array('phptagopen', 'phptagclose', 'backslashtmp'), array('<?php', '?>', '\\'), $str); //<? $line = explode("<br />", rtrim(ltrim($str,'<code>'),'</code>')); $result = '<div class="code"><ol>'; foreach($line as $key=>$val) { $result .= '<li>'.$val.'</li>'; } $result .= '</ol></div>'; $result = str_replace("\n", "", $result); if( $show!== false) { echo($result); }else { return $result; } }代码高亮使用方法highlight_code($str,true);$str是输入的字符串,最新更新后的下载地址是http://pan.baidu.com/share/link?shareid=136459&uk=3172762343 欢迎大家下载测试
发表评论 取消回复