I've put together a small piece of code for use in the HTML HEAD tag to automate the creation of the content for a META DESCRIPTION tag.
I would be interested if anyone can see a neater way to do it or solve my regex TODO. And otherwise I'm posting just in case this is helpful for anyone else.
<meta name="description" content='<?php // Check if there is text in the summary field, if so output it as the author // has deliberately written a meta description. Otherwise grab the first N // (e.g. 160) characters of the body field, strip the HTML tags, replace them // with a space and then output it as a poor-mans meta description. // // TODO this regex replaces opening and closing HTML tags and so // <h2>What are hedgehogs?</h2><p>Hedgehogs are // is cleaned up like this // _What are hedgehogs?__Hedgehogs are // when the ideal would be // What are hedgehogs?_Hedgehogs are $summary = $page->get("summary"); if($summary) echo $summary; else echo preg_replace('/<[^>]*>/', ' ', substr($page->get("body"), 0, 160)); ?>' />