请帮助
代码php将div id保存到文件txt 代码php将div id保存到文件txt
<html dir="ltr">
<head>
<meta http-equiv="Content-Language" content="utf-8">
<title>---</title>
<?php
?>
</head>
<body>
<div id="ab">
welcome<p>
welcome<br>
</p>
</div>
</body>
</html>
答案 0 :(得分:0)
不幸的是,您无法直接调用div并将其保存为php上的文本文件,但您可以使用ob_start来传输缓冲区并将内容另存为文本文件
<body>
<div id="ab">
<?php
// Start the buffering //
ob_start();
?>
welcome
<p>Welcome</p>
<?php
$output = ob_get_contents();
ob_end_clean();
// Get the content that is in the buffer and put it in your file //
file_put_contents('Test.txt', ob_get_contents());
?>
</div>
您也可以使用Javascript作为替代,请参阅下面的jQuery示例:
<input type="button" value="Export" id="btnExport" />
<script>
$(document).ready(function(){
$('#btnExport').click(function(){
var contents = $('#ab').text();
$(this).href = 'data:text/plain;charset=utf-8,' + encodeURIComponent(contents );
$(this).attr("download","test.txt");
});
});
</script>