php.lover Posted December 8, 2009 Share Posted December 8, 2009 hi I want a piece of code which will get the content of meta "description" tag (in <head></head> of the page) and echos it. <meta name="description" content="this part will be echoed" /> Kind Regards Link to comment https://forums.phpfreaks.com/topic/184393-how-to-echo-description-meta-tag-content/ Share on other sites More sharing options...
cags Posted December 8, 2009 Share Posted December 8, 2009 Many ways to do it. First you need the content of the site, the simplest way to do that is probably get_file_contents. You will then need to parse the string. If all you want is the content of the meta tag then possibly using preg_match would be appropriate, it thats just the first thing you wish to get and you then wish to get other things you would be much better off using some sort of DOMDocument. Link to comment https://forums.phpfreaks.com/topic/184393-how-to-echo-description-meta-tag-content/#findComment-973371 Share on other sites More sharing options...
php.lover Posted December 8, 2009 Author Share Posted December 8, 2009 thanks a lot for your quick reply. yes I want just the content of this tag. but I'm very new to php so if possible please guide me more detailed Link to comment https://forums.phpfreaks.com/topic/184393-how-to-echo-description-meta-tag-content/#findComment-973373 Share on other sites More sharing options...
cags Posted December 8, 2009 Share Posted December 8, 2009 I think this should be about right... $pattern = '#<meta (?=[^>]*name="description")[^>]*content="(\K[^"]+)#'; $input = file_get_contents('URL HERE'); preg_match($pattern, $input, $out); echo $out[0]); NB: I accidently used get_file_contents in my last post which should of course have been file_get_contents. Link to comment https://forums.phpfreaks.com/topic/184393-how-to-echo-description-meta-tag-content/#findComment-973378 Share on other sites More sharing options...
php.lover Posted December 8, 2009 Author Share Posted December 8, 2009 I think this should be about right... $pattern = '#<meta (?=[^>]*name="description")[^>]*content="(\K[^"]+)#'; $input = file_get_contents('URL HERE'); preg_match($pattern, $input, $out); echo $out[0]); NB: I accidently used get_file_contents in my last post which should of course have been file_get_contents. Thank you very much, that worked. First I used this code to produce the url of the current page: function curPageURL() { $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";} $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; } return $pageURL; } then I used your code as follows: $pattern = '#<meta (?=[^>]*name="description")[^>]*content="(\K[^"]+)#'; $input = file_get_contents(curPageURL()); preg_match($pattern, $input, $out); echo ($out[0]); Thanks again my friend... Link to comment https://forums.phpfreaks.com/topic/184393-how-to-echo-description-meta-tag-content/#findComment-973772 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.