Open HTML file, and Extract HTML Content

Discussion in 'PHP' started by FishSword, Aug 7, 2010.

  1. #1
    Hi,

    How do I open all HTML file contained in a folder, then search for the title of the page, and save the title in the database?
    Also, is it possible to do this using functions?

    Cheers.
     
    FishSword, Aug 7, 2010 IP
  2. Thorlax402

    Thorlax402 Member

    Messages:
    194
    Likes Received:
    2
    Best Answers:
    5
    Trophy Points:
    40
    #2
    Use file_get_contents() to get the contents of the html file as a string. Then you can use this function to get the title. Use the command get_tag_contents($html_content, 'title'):

    
    function get_tag_contents($string, $tag) {
    	$startTag = "<{$tag}>";
    	$endTag = "</{$tag}>";
    	$start = strpos($string, $startTag);
    	if ($start !== false) {
    		$length = strpos($string, $endTag) - $start;
    		return substr($string, $start, $length);
    	} else {
    		return "Tag not found.";
    	}
    }
    
    PHP:
     
    Thorlax402, Aug 7, 2010 IP