0

Possible Duplicate:
Help getting meta title and description

I've spend a full day on it. Searched on the net. Saw some similar questions on satckoverflow also. but all I got disappointment.

I want to get some php code by which I can output the title and some 4-5 lines for the description of any website using php.

Community
  • 1
  • 1
Muddser
  • 89
  • 1
  • 2
  • 7
  • 6
    What do you mean when you say description? – MeLight Oct 09 '11 at 11:15
  • 1
    This is what you need http://stackoverflow.com/questions/6113716/help-getting-meta-title-and-description/6113811#6113811 – Mob Oct 09 '11 at 11:17
  • Define "title" and "description" in this case. For the `title` element in an HTML page you can just get the page and use a DOM parser to extract that tag. (If the tag exists on that page, which it might not, and if the tag is defined correctly, which it might not be, and if the page is even HTML, which it might not be, etc.) But what is a "description"? Where do you expect to get that? If it's something in a `meta` tag then expect it to not be there even more often than the `title` tag. – David Oct 09 '11 at 11:18
  • 1
    I guess he meant the meta description. Anyways, is this something you were looking for? : http://php.net/manual/de/function.get-meta-tags.php – r0skar Oct 09 '11 at 11:19
  • 2
    it would have been nice to see what you have tried after spending a full day researching the topic … instead of now asking to give teh codez. That would have allowed us to point out where you might have missed something. – Gordon Oct 09 '11 at 11:40
  • *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Oct 09 '11 at 12:01
  • sorry for wrong doing... but please help me... – Muddser Oct 09 '11 at 14:27

2 Answers2

6
<?php

  $url = "http://www.drquincy.com/";

  $fp = fopen($url, 'r');
  $content = "";
  while(!feof($fp)) {
        $buffer = trim(fgets($fp, 4096));
        $content .= $buffer;
  }


  $start = '<title>';
  $end = '</title>';
  preg_match(" / $start( . * )$end / s", $content, $match);
  $title = $match[1];
  $metatagarray = get_meta_tags($url);
  $keywords = $metatagarray["keywords"];
  $description = $metatagarray["description"];
  echo " <div><strong>URL: </strong >$url</div> \n";
  echo " <div><strong>Title: </strong >$title</div> \n";
  echo " <div><strong>Description: </strong >$description</div>\n";
  echo " <div><strong>Keywords: </strong >$keywords</div>\n";

Just change the url:)

Dzoki
  • 739
  • 4
  • 14
  • it isn't working... don't knw what s wrong?? – Muddser Oct 09 '11 at 14:11
  • Chances are that this wont have the desired result your looking for. It will work, but the title will likely be "Browser Not Supported" or something like that. You'll want to send headers (you can do this with fopen) – Adam Fowler Oct 09 '11 at 19:17
  • it says: Warning: preg_match() [function.preg-match]: Unknown modifier 't' in C:\wamp\www\1.php on line 27 Call Stack # Time Memory Function Location 1 0.0005 373336 {main}( ) ..\1.php:0 2 3.5318 411744 preg_match ( ) ..\1.php:27 ( ! ) Notice: Undefined index: keywords in C:\wamp\www\1.php on line 30 Call Stack # Time Memory Function Location 1 0.0005 373336 {main}( ) ..\1.php:0 ( ! ) Notice: Undefined index: description in C:\wamp\www\1.php on line 31 Call Stack # Time Memory Function Location 1 0.0005 373336 {main}( ) ..\1.php:0 URL: http://www.drquincy.com/ Title: Description: Keywords: – Muddser Oct 10 '11 at 14:53
0

There are many ways to parse a HTML. First, you want the content itself:

$res = file_get_contents("http://www.google.com");

That assumes file_get_contents allowed to access uri. For example, you might utilize a regex:

preg_match("~<title>(.*?)</title>~", $res, $match);
$title = $match[1];

But it would be better to use a DOM parser. http://php.net/manual/en/book.xml.php, though that may be problem if the target content is not valid xml.

Mikulas Dite
  • 7,790
  • 9
  • 59
  • 99