0

After looking around for something like octopress in php and not finding anything, I decided to create something myself in php that would do the trick.

I'd like to start with writing some code in php that reads php files and can extract meta-data from them, so I can build an archive page of blog posts, etc.

I thought I could create yaml files, and include php/html in these files for the main content of the blog posts, but it's not clear to me if this is possible at all? Googling around for "use php in yaml" didn't really get me much further.

So I thought I'd ask here what the best approach would be for doing something like this.

Can anyone help?

Thanks B

b20000
  • 995
  • 1
  • 12
  • 30

2 Answers2

1

I am not familiar with yaml - can you simply use PHP's get meta tags?

<?php
// Assuming the above tags are at www.example.com
$tags = get_meta_tags('http://www.example.com/');

// Notice how the keys are all lowercase now, and
// how . was replaced by _ in the key.
echo $tags['author'];       // name
echo $tags['keywords'];     // php documentation
echo $tags['description'];  // a php manual
echo $tags['geo_position']; // 49.33;-86.59
var_dump($tags);// See any and all meta tags that have been picked up.
?>

Edit: I added the var_dump in so you can see all the tags you get. Test it out on the page you want to hit.

Fluffeh
  • 33,228
  • 16
  • 67
  • 80
  • 1
    can this also work for non standard tags? I basically need to store a variety of info about the php file i'm reading in the file itself, sort of like a header .... – b20000 Aug 27 '12 at 07:17
  • @b20000 If your page has the meta tags, the `get_meta_tags()` will return the contents in the array? – Fluffeh Sep 06 '12 at 11:31
  • is this better than putting some kind of yaml header in the file? – b20000 Sep 17 '12 at 20:11
0
<?php
header('Content-Type:text/html; charset=utf-8');
$tags = get_meta_tags('http://www.narenji.ir'); 
var_dump($tags);
?>

Output is
array
  'keywords' => string 'اخبار, تکنولوژی, نارنجی, گجت, فناوری, موبایل, خبر, تبلت, لپ تاپ, کامپیوتر, ربات, مانیتور, سه بعدی, تلویزیون' (length=186)
  'description' => string 'مکانی برای آشنایی با ابزارها و اخبار داغ دنیای فناوری' (length=97)

Or you can use following code

<?php
$url = 'http://www.example.com/';

if (!$fp = fopen($url, 'r')) {
    trigger_error("Unable to open URL ($url)", E_USER_ERROR);
}

$meta = stream_get_meta_data($fp);

print_r($meta);

fclose($fp);
?>

If your source file is image then you can try with it

<?php
echo "test1.jpg:<br />\n";
$exif = exif_read_data('tests/test1.jpg', 'IFD0');
echo $exif===false ? "No header data found.<br />\n" : "Image contains headers<br />\n";

$exif = exif_read_data('tests/test2.jpg', 0, true);
echo "test2.jpg:<br />\n";
foreach ($exif as $key => $section) {
    foreach ($section as $name => $val) {
        echo "$key.$name: $val<br />\n";
    }
}
?>
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37