Home  >  Article  >  php教程  >  PHP动态加载html文件之后<head>中内容全部移动到<body&

PHP动态加载html文件之后<head>中内容全部移动到<body&

WBOY
WBOYOriginal
2016-06-06 19:50:45956browse

后来在CSDN中找到一篇文章,介绍了这个问题,按照这个问题的解决思路,我最终很方便地就解决了这个问题。 但是,我也不清楚我的文档哪个被我手贱使用windows的文本编辑器编辑过,一个一个找又麻烦,所以这里提供我在网上找到的一个php文档,将他保存为php然


        后来在CSDN中找到一篇文章,介绍了这个问题,按照这个问题的解决思路,我最终很方便地就解决了这个问题。

        但是,我也不清楚我的文档哪个被我手贱使用windows的文本编辑器编辑过,一个一个找又麻烦,所以这里提供我在网上找到的一个php文档,将他保存为php然后放在你所要清理的文件目录的根目录,然后使用浏览器访问这个文件,就会自动清楚该目录树下所有具有BOM的文档的头部标记。亲测有效。

<?php $basedir = str_replace('/clearBOM.php','',str_replace('\\','/',dirname(__FILE__)));
$auto = 1;
checkdir($basedir);
function checkdir($basedir){
	if ($dh = opendir($basedir)) {
		while (($file = readdir($dh)) !== false) {
			if ($file != '.' && $file != '..'){
				if (!is_dir($basedir.'/'.$file)) {
					$filename = $basedir.'/'.$file;
					echo 'filename:'.$basedir.'/'.$file.checkBOM($filename).'<br>';
				} else {
					$dirname = $basedir.'/'.$file;
					checkdir($dirname);
				}
			}
		}
		closedir($dh);
	}
}

function checkBOM ($filename) {
	global $auto;
	$contents = file_get_contents($filename);
	$charset[1] = substr($contents, 0, 1);
	$charset[2] = substr($contents, 1, 1);
	$charset[3] = substr($contents, 2, 1);
	if (ord($charset[1]) == 239 && ord($charset[2]) == 187 && ord($charset[3]) == 191) {
		if ($auto == 1) {
			$rest = substr($contents, 3);
			rewrite ($filename, $rest);
			return '<font color="red">BOM found,automatically removed.</font>';
		} else {
			return '<font color="red">BOM found.</font>';
		}
	} else {
		return 'BOM Not Found.';
	}
}

function rewrite ($filename, $data) {
	$filenum = fopen($filename, 'w');
	flock($filenum, LOCK_EX);
	fwrite($filenum, $data);
	fclose($filenum);
}
?>

            以上PHP无需进行任何修改。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn