I am trying to create a feature on my website where users can upload links, like Digg. I have code that grabs the HTML source code from the URL that a user uploads to my website, and stores it in a .txt file. Then I want to grab the contents in the tag
<meta name="content" description="GRAB THIS">
assuming this tag exists. Sometimes it works, but other times it doesn’t work, even though the source code for that particular webpage contains the necessary meta tag exactly as I have specified in my code. I’ve noticed that it seems to not work correctly if the “GRAB THIS” content contain html entities (&, etc.). Please let me know if you have any ideas on how to get this to work. Here is my code:
$html_data = file_get_contents( $path_to_txt_file_that_contains_html );
preg_match( '#<meta name="description" content="(.+?)">#si', $html_data, $tor;
$tor = str_replace ( '<meta name="description" content="' , "", $tor[0] );
$tor = str_replace ( '">', "", $tor );
Sometimes $tor still contains
<meta name="description" content="CONTENT"
but without the closing >, so my code breaks once I put this in a mySQl database. Any ideas on what I’m doing wrong? Thanks ahead of time for any help!
It’s actually extremely simple.
PHP offers it’s own built in solution: http://php.net/manual/en/function.get-meta-tags.php