php正则替换,为什么多出</html>?运行test.php,得到的结果是
<body>
hello
</body>
</html>
为什么多出</html>?test.html的代码如下<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- Source is http://www.ccw.com.cn/ -->
<head>
</head>
<body>
hello
</body>
</html>
test.php代码如下<?php
$file=file_get_contents("test.htm");
$file=preg_replace("#.*(<body[^>]*>.*</body>).*#isU","$1",$file);
echo $file;
?>
<body>
hello
</body>
</html>
为什么多出</html>?test.html的代码如下<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- Source is http://www.ccw.com.cn/ -->
<head>
</head>
<body>
hello
</body>
</html>
test.php代码如下<?php
$file=file_get_contents("test.htm");
$file=preg_replace("#.*(<body[^>]*>.*</body>).*#isU","$1",$file);
echo $file;
?>
解决方案 »
- 请问用PHP 怎么获取msn好友 邮箱列表
- PHP上传文件强制转换文件名
- 【新手】请教一个逻辑上的问题。
- 输出sql的问题
- php中html的显示问题
- 这个计时系统用什么做好啊?
- 急!昨天换了xp系统,结果php装好了,mysql却连不上……
- 我的Mysql数据库可以读出数据,但不可以插入数据,怎么办?
- $connect=mysql_connect('localhost','root','summer');中一定要用localhost吗!怎么才能把页面和数据库分放到两台不同机器上,可行吗?
- PHP验证码例程 在我机器上显示为一个红叉,请高手看看这是什么问题啊?
- 小问题!
- 关于IIS7.0 FASTCGI执行PHP超时
在正则的最后加一个结束符$,应该就能解决问题了,匹配到结尾
试试看
<?php
$file=file_get_contents("test.htm");
$file=preg_replace("#.*(<body[^>]*>.*$</body>).*#isU","$1",$file);
echo $file;
?>
<?php
$html=file_get_contents("test.html");
preg_match('{<body(.*)</body>}is', $html, $body);
print_r($body[0]);
?>
preg_match('#(<body[^>]*>.*</body>)#isU', $file, $arr);
$file = $arr[1];echo $file;
echo $file;<body>
hello
</body>