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>