同时替换<?php和<?||file_get_contents的使用
2012-04-06 15:28
507 查看
<?php class _CCTG extends CController { public function __init() { $this->set_page_base("abc"); return array( _METHOD('index',array(),false,false), ); } public function __ready() { return TRUE; } public function index() { $this->set_page(FALSE); $file_name = '/libs/controller.class.php'; $data = file_get_contents($_SERVER["DOCUMENT_ROOT"].$file_name); if(! $data) { echo "sys error!"; } else { $data = str_replace('<'.'?php', '<start>', $data); //如果这样$data = str_replace('<?php', '<start>', $data);的话, //可以替换掉<?php,但是碰到$data = str_replace('<?', '<start>', $data); //就没办法替换了。就算加上一句替换<?的都没效果。 $data = str_replace('<'.'?', '<start>', $data); $data = str_replace('?>', '<end>', $data); echo "<pre>"; $this->highlight_code(print_r($data), $show=true); echo "</pre>"; echo "<fieldset>"."<legend>"."代码高亮"."</legend>"; highlight_file($_SERVER["DOCUMENT_ROOT"].$file_name); echo "</fieldset>"; } } function error($code, $message) { echo "error:($code) $message"; return false; } //从thinkphp3.0里抠出来的代码高亮(不知道怎么的没效果) 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 later $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; } } function __finally() { } } ?>
相关文章推荐
- file_get_contents("php://input")的使用方法
- file_get_contents("php://input")的使用方法
- 关于file_get_contents("php://input")
- PHP:file_get_contents的代理使用方法
- PHP中使用file_get_contents post数据代码例子
- file_get_contents("php://input")的使用方法
- PHP中curl_init和file_get_contents配合使用
- PHP使用curl替代file_get_contents
- file_get_contents("php://input")的使用方法
- file_get_contents("php://input")的使用方法
- PHP使用file_get_contents或curl请求https的域名内容为空或Http 505错误的问题排查方法
- file_get_contents("php://input")的使用方法
- php中使用Curl、socket、file_get_contents三种方法POST提交数据
- file_get_contents("php://input")的使用方法
- file_get_contents("php://input")的使用方法
- PHP下通过file_get_contents的代理使用方法
- php 使用file_get_contents读取大文件的方法
- PHP使用fopen与file_get_contents读取文件实例分享
- file_put_contents以及file_get_contents的用法与在使用过程中遇到的问题(PHP学习)
- file_get_contents("php://input")的使用方法