-2

please help

code php save div id to file txt code php save div id to file 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>
chris85
  • 23,846
  • 7
  • 34
  • 51

1 Answers1

0

Unfortunately you can't directly call div and save it as text file on php but you can use ob_start to stream buffer and save the content as text file

<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>

You can also use a Javascript as alternative, see below jQuery example:

<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> 
Fernan Vecina
  • 144
  • 1
  • 8