请问怎样用php重写一个htm文件? 本帖最后由 mingfish 于 2009-09-28 16:33:53 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 高手们写一段嘛,modify.php该怎么写? 给你一段ajax实现的代码a.html<script type="text/javascript">var request=null;function createRequest(){ try{ request=new XMLHttpRequest(); } catch(trymicrosoft){ try{ request=new ActiveXObject("Msxml2.XMLHTTP"); } catch(othermicrosoft){ try{ request=new ActiveXObject("Microsoft.XMLHTTP"); } catch(failed){ request=null; } } } if(request==null) alert("Error creating request object");}function modify(){ createRequest(); var url="modify.php"; request.open("GET",url,true); request.onreadystatechange=updatePage; request.send(null);}function updatePage(){ if(request.readyState==4){ if(request.status==200){ document.getElementsByTagName("div")[0].innerHTML=request.responseText; } }}</script><span>hello</span> <div class="selected"> aaaa </div> <input type="button" onclick="modify()" value="js dom 更新htm文件"/>modify.php<?phpecho "bbbb";?> 谢谢TENG0210,我不是这个意思,我是要完全地改写a.html中的代码,就是说点击了按钮后,双击打开a.html,里面的代码都已经是bbbb了。你给的这段代码用jquery直接 $(".selected").html("bbbb")就完成了,无需ajax 实际上你是想用php修改个html文件?1.考虑dom,html一般都严格匹配标签,推荐用这个.2.考虑正则,会比较麻烦$dom = new domdocument;$html = <<<EOD<html><body><span>hello</span><div class="selected">aaaa</div><button>改写该htm文件</button></body></html>EOD;$dom->loadXML($html);$dd = simplexml_import_dom($dom);$dd->body->div = "bbbb";echo $dom->saveXML(); 写了modify.php的内容,可以实现你要的功能,拓展性不好,见谅<?php//header("Content-type:text/html;Charset=gbk");$fp=fopen('a.html','r');if($fp){ $str=''; while(($tmp=fread($fp,8196))){ $str.=$tmp; } fclose($fp); $pos=strpos($str,"<div class=\"selected\">"); $pos+=strlen("<div class=\"selected\">"); $head=substr($str,0,$pos); $pos=strpos($str,"</div>"); $tail=substr($str,$pos,strlen($str)-$pos); $finsh=$head."bbbb".$tail;}else{ echo "error"; exit;}$fp=fopen('a.html','w');if($fp){ fwrite($fp,$finsh); fclose($fp);}else{ echo "error"; exit;}?> 谢谢,大概看的明白,本来打算用js配合正则改写html然后保存的,原来实现起来是这般困难。 求高手帮忙把这段asp代码转换成php的... 关于PHPexcel读取文件问题 问一个关于用header重定向的问题 相当与ASP中的函数clnt,clng 有个郁闷的问题,应该属于设置的问题吧,不知道是不是PHP5主PHP4的关系 php curl post提交抓取内容问题 如何利用网页打开浏览者硬盘中的指定文件 怎么同时删除/改名某个目录下的指定名字的文件?(继。。。。。) sos!!!高手---关于怎样远程读写文件! 如何在apache中设置mysql的自动启动? where条件该如果修改 请教高人jquery+json+ajax问题??
a.html<script type="text/javascript">
var request=null;
function createRequest(){
try{
request=new XMLHttpRequest();
} catch(trymicrosoft){
try{
request=new ActiveXObject("Msxml2.XMLHTTP");
} catch(othermicrosoft){
try{
request=new ActiveXObject("Microsoft.XMLHTTP");
} catch(failed){
request=null;
}
}
} if(request==null)
alert("Error creating request object");
}function modify(){
createRequest();
var url="modify.php";
request.open("GET",url,true);
request.onreadystatechange=updatePage;
request.send(null);
}function updatePage(){
if(request.readyState==4){
if(request.status==200){
document.getElementsByTagName("div")[0].innerHTML=request.responseText;
}
}
}
</script>
<span>hello</span>
<div class="selected">
aaaa </div>
<input type="button" onclick="modify()" value="js dom 更新htm文件"/>
modify.php<?php
echo "bbbb";
?>
你给的这段代码用jquery直接 $(".selected").html("bbbb")就完成了,无需ajax
1.考虑dom,html一般都严格匹配标签,推荐用这个.
2.考虑正则,会比较麻烦$dom = new domdocument;
$html = <<<EOD
<html><body><span>hello</span><div class="selected">aaaa</div><button>改写该htm文件</button></body></html>
EOD;
$dom->loadXML($html);
$dd = simplexml_import_dom($dom);
$dd->body->div = "bbbb";
echo $dom->saveXML();
//header("Content-type:text/html;Charset=gbk");
$fp=fopen('a.html','r');
if($fp){
$str='';
while(($tmp=fread($fp,8196))){
$str.=$tmp;
}
fclose($fp);
$pos=strpos($str,"<div class=\"selected\">");
$pos+=strlen("<div class=\"selected\">");
$head=substr($str,0,$pos);
$pos=strpos($str,"</div>");
$tail=substr($str,$pos,strlen($str)-$pos);
$finsh=$head."bbbb".$tail;
}else{
echo "error";
exit;
}
$fp=fopen('a.html','w');
if($fp){
fwrite($fp,$finsh);
fclose($fp);
}else{
echo "error";
exit;
}
?>