Function:
function meta_content($str, $name)
{
$meta_str = '<meta name="' . $name . '"';
$cnt_str = 'content="';
if (($meta_str_pos = strpos($str, $meta_str)) !== false) {
$cnt_str_pos = strpos($str, $cnt_str, $meta_str_pos);
$cnt_pos = $cnt_str_pos + strlen($cnt_str);
$quote_pos = strpos($str, '"', $cnt_pos);
$content = substr($str, $cnt_pos, $quote_pos - $cnt_pos);
return $content;
} else {
return 'n/a';
}
}
Usage:
$data = '<meta name="keywords" content="php, mysql, php templates,
apache, php manual, server, pdf, database, flash, phpbuilder,
content management system, sql, script, oracle, string, xml,
regular expressions, php5, webalizer, php tutorials, code,
nusoap, classes, developers"><meta name="description"
content="some description about php and other stuff.">';
$keywords = meta_content($data, 'keywords');
echo $keywords . '<br />';
$description = meta_content($data, 'description');
echo $description . '<br />';
Output:
php, mysql, php templates, apache, php manual, server, pdf, database,
flash, phpbuilder, content management system, sql, script, oracle,
string, xml, regular expressions, php5, webalizer, php tutorials, code,
nusoap, classes, developers
some description about php and other stuff.