Page 1 of 1

how to echo "description" meta tag content?

Posted: Tue Dec 08, 2009 7:13 am
by php.lover
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.

Code: Select all

<meta name="description" content="this part will be echoed" />
Kind Regards

Re: how to echo "description" meta tag content?

Posted: Tue Dec 08, 2009 9:58 am
by incubi
Well it's cheesy but this may work!

Code: Select all

 
$html = '<meta name="description" content="this part will be echoed" />';
preg_match('/content.*? \/>/', $html, $matches);
$s = str_replace('content="',"", $matches[0]);
$s = str_replace('" />',"", $s);
echo $s;
 

incubi

Re: how to echo "description" meta tag content?

Posted: Tue Dec 08, 2009 1:09 pm
by AbraCadaver
If the page is a file or at a URL, try:

Code: Select all

$meta = get_meta_tags($filename);
echo $meta['description'];

Re: how to echo "description" meta tag content?

Posted: Tue Dec 08, 2009 3:48 pm
by php.lover
I found the solution.

Here is the code:

Code: Select all

<?php 
                    
                    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;
} 
 
                    $pattern = '#<meta (?=[^>]*name="description")[^>]*content="(\K[^"]+)#';
$input = file_get_contents(curPageURL());
preg_match($pattern, $input, $out);
echo ($out[0]);
                    
                    ?>
Thanks for your guides.

Re: how to echo "description" meta tag content?

Posted: Tue Dec 08, 2009 3:54 pm
by AbraCadaver
Uhhh, O.K. I guess. But why? That's ridiculous!

Code: Select all

$meta = get_meta_tags(curPageURL());
echo $meta['description'];

Re: how to echo "description" meta tag content?

Posted: Wed Dec 09, 2009 2:05 am
by php.lover
:)

I have a new problem.

these codes work if the allow_url_fopen is enabled on the server. and enabling it is a security risk.

I think it will be better to use curl.
but I couldn't change that part of code.
please help me to do this cahnge....thanks

Re: how to echo "description" meta tag content?

Posted: Wed Dec 09, 2009 2:47 am
by jackpf

Re: how to echo "description" meta tag content?

Posted: Wed Dec 09, 2009 7:38 am
by AbraCadaver
php.lover wrote::)

I have a new problem.

these codes work if the allow_url_fopen is enabled on the server. and enabling it is a security risk.

I think it will be better to use curl.
but I couldn't change that part of code.
please help me to do this cahnge....thanks
Enabling almost anything is a security risk. You need to weigh the risks versus benefits. Normally the benefits will win here as the risk is very small. I always allow_url_fopen.