By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,636 Members | 2,334 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,636 IT Pros & Developers. It's quick & easy.

Eregi() to extract author meta tag?

P: n/a
Hi

I took a quick look in the archives, but didn't find an answer
to this one.

I'd like to display a list of HTML files in a directory, showing the
author's name between brackets after the file name. I can successfully
extract the TITLE section, but no luck with the AUTHOR part. Any idea
why?

<?php
$body = "<h1>Documents</h1>";

$dir = opendir(".");
while ($item = readdir($dir)) {
//Only HTM(L) files
if(substr($item,-5) == ".html" || substr($item,-4) == ".htm") {
$fp = fopen($item, "r");
$contents = fread($fp, filesize($item));
fclose($fp);

//Works OK to extract TITLE
eregi("<title>(.+)</title>", $contents, $regs);
$file[$item] = $regs[1];

//Doesn't work
//eregi("<meta name=\"author\" content=\"(.+)\">', $contents,
$regs);

//Doesn't work
eregi('<meta name="author" content="(.+)">', $contents, $regs);
If ($regs[1])
$author[$item] = "(Author unknown)";
else
$author[$item] = "(" . $regs[1] . ")";
}
}

$body .= "<ul>\n";
foreach ($file as $item => $title) {
$body .= "<li><a href=\"" . $item . "\">" . $file[$item] . "</a> " .
$author[$item];
}
$body .= "</ul>\n";

print $body;

?>

FWIW, I tried making eregi non-greedy using (.+?) or ([^"].+), and
also used double quotes (shown above), all to no avail.

Any tip much appreciated.

Thank you
JD.
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Jane Doe wrote:
I'd like to display a list of HTML files in a directory, showing the
author's name between brackets after the file name. I can successfully
extract the TITLE section, but no luck with the AUTHOR part.


Consider get_meta_tags.

http://www.php.net/manual/en/function.get-meta-tags.php

And remember there's no requirement for an author to (a) show her
real name; or (b) use the so-called "author" meta tag, or any meta
tag for that matter.

--
Jock
Jul 17 '05 #2

P: n/a
On Fri, 17 Oct 2003 18:26:04 +0100, John Dunlop
<jo*********@johndunlop.info> wrote:
http://www.php.net/manual/en/function.get-meta-tags.php
Thx John :-)

BTW, is there a similar API to extract the title section? It's kinda
stupide to have PHP parse the same document twice to extra this and
the meta tags:

$fp = fopen($item, "r");
$contents = fread($fp, filesize($item));
fclose($fp);
eregi("<title>(.+)</title>", $contents, $regs);
if (!$regs[1]) {
$title = "(Title not filled)";
} else {
$title = $regs[1];
}

$tags = get_meta_tags($item);

if (!$tags['author']) {
$author = "(Author not filed)";
} else {
$author = "(" . $tags['author'] .")";
}
print "<li><a href=\"$item\">$title</a> $author";
And remember there's no requirement for an author to (a) show her
real name; or (b) use the so-called "author" meta tag, or any meta
tag for that matter.


It's OK. I run a script prior to accepting a document inside our CMS,
and reject any document that doesn't have the TITLE and AUTHOR
sections filled.

Thx :-)
JD.
Jul 17 '05 #3

P: n/a
On Fri, 17 Oct 2003 19:48:18 +0200, Jane Doe <ja******@acme.com>
wrote:
BTW, is there a similar API to extract the title section? It's kinda
stupide to have PHP parse the same document twice to extra this and
the meta tags:


Stupid me :-) Just use the "description" meta tag, and be done with
it.

Thx again
JD.
Jul 17 '05 #4

P: n/a
Jane Doe <ja******@acme.com> wrote in message
news:<8b********************************@4ax.com>. ..

I'd like to display a list of HTML files in a directory, showing the
author's name between brackets after the file name. I can successfully
extract the TITLE section, but no luck with the AUTHOR part. Any idea
why?


Let me start with the obvious, but rather dumb, question: are you
sure that the tags are there? If they are, recall that PHP has
a function that does exactly what you want:

http://www.php.net/get_meta_tags

Cheers,
NC
Jul 17 '05 #5

P: n/a
On 17 Oct 2003 11:03:27 -0700, nc@iname.com (Nikolai Chuvakhin) wrote:
Let me start with the obvious, but rather dumb, question: are you
sure that the tags are there?
Yes, since I'm the one proofreading docs before uploading them :-)
If they are, recall that PHP has >a function that does exactly what you want:
http://www.php.net/get_meta_tags


Thx, that did it :-)

JD.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.