您的位置:首页 > 编程语言 > PHP开发

同时替换<?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()
{
}

}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: