how to echo "description" meta tag content?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php.lover
Forum Newbie
Posts: 3
Joined: Tue Dec 08, 2009 7:02 am

how to echo "description" meta tag content?

Post 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
incubi
Forum Contributor
Posts: 119
Joined: Mon Dec 07, 2009 1:47 pm

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

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

If the page is a file or at a URL, try:

Code: Select all

$meta = get_meta_tags($filename);
echo $meta['description'];
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
php.lover
Forum Newbie
Posts: 3
Joined: Tue Dec 08, 2009 7:02 am

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

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

Uhhh, O.K. I guess. But why? That's ridiculous!

Code: Select all

$meta = get_meta_tags(curPageURL());
echo $meta['description'];
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
php.lover
Forum Newbie
Posts: 3
Joined: Tue Dec 08, 2009 7:02 am

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

Post 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
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post by jackpf »

User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply