Jump to content

how to echo "description" meta tag content?


php.lover

Recommended Posts

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.

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.

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...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.