Jump to content

get meta description tag content...almost there...i think


Recommended Posts

I'm trying to extract the Meta Description content using REGEX but its giving me some trouble. It will find the content=" and echo that out but can;t seem to get it to get the end part of the tag which could include " /> or "> or "/> i have got ((\b\"(.)>\i*)) . I have been using this tool http://www.gskinner.com/RegExr/ which rocks!

 

<?php
function strip($file, $start_mark, $end_mark){

$start = strpos ($file, $start_mark);

$end = strpos ($file, $end_mark, $start);

$text = substr ($file, $start, $end - $start);

$text = str_replace ($start_mark, '', $text);

$text = str_replace ($end_mark, '', $text); 

return $text;
}


$file = file_get_contents($_GET['url']);

// get the description
$desc = preg_match('(\<(/?meta name="description"[^\>]+)\>)', $file, $fulldesc);
//echo "Match Content:<br/><br/>".$dstart[1]."<br/><br/>";
echo $fulldesc[1];

//get the title
$descstart = preg_match("((\bcontent=\"\i*))", $fulldesc[1], $dstart);
echo $dstart[1]."<br/>";

$descend = preg_match("((\b\"(.)>\i*))", $fulldesc[1], $dend);
echo $dend[1]."<br/>";

$description= strip($fulldesc[1], $dstart[1], $dend[1]);
echo "This is the match:<br/><br/> $description";

negative character classes are just like positive character classes: they only match alternatives or ranges for one character match.  So when you do [^\>]  you aren't matching anything that's not "\>"  you're matching anything that's not "\" or ">"

 

You can use lookahead for that, or you can look at it as "no matter what, the end of the tag is ">" so why do I need to even check whether it has a \ or / there or not?" and just do [^>]

thanks for that... i still couldn;t get it to work but i found this one...

 

(<meta[\s]+[^>]*?name[\s]?=[\s\"\']description[\s\"\']+content[\s]?=[\s\"\']+(.*?)[\"\']+.*?>)

 

which seems to work well.

 

I'm also trying to get the title tag contents which is working but it won;t find capital versions or with encoded HTML...this is what i have

 

$pagetitle = preg_match("(<title>(.*)<\/title>)", $file, $t);
$title = $t[1];

 

this is the tag it won't process

 

<TITLE>MySite-  bla bla &#038; reviews about stuff</TITLE>

 

thanks

 

Lee

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.