<?php
$text = '<title>Hello</title><head>Hhsjfjjdf</head><h1>Hrrrrrr</h1>';
$fp = fopen("file.txt", "w");
preg_match_all("#(<title>.*<\/title>).*(<h1>.*<\/h1>)#is",$text,$math);
fwrite($fp, $math[1][1]);
fwrite($fp, $math[2][1]);
fclose($fp);
?>
preg_match_all("#(<title>.*<\/title>).*(<h1>.*<\/h1>)#is",$text,$math);