Wednesday, February 23, 2011

Stripping Tags From a Webpage

Ever need to take a beautifully formed webpage and strip out all the tags and junk to just see the text?
It is really pretty easy with the PHP strip_tags() function. The only catch is that when all the tags are removed, it leaves a pile of attributes. Which if you just need to strip out the text from a simple XML doc or something pretty basic it works great. 
Here is the code. Just name the file justtext.php and have all the fun you can stand.


<?php
if (!isset($_POST["url"])){ 
    echo "Enter URL: <form method='post' action='justtext.php'><input type='text' name='url'><input type='submit' value='submit'></form> ";
      }
else{echo strip_tags(file_get_contents($_POST["url"] ));
      }
?>

No comments: