alwoodman Posted March 12, 2009 Share Posted March 12, 2009 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"; Quote Link to comment https://forums.phpfreaks.com/topic/149136-get-meta-description-tag-contentalmost-therei-think/ Share on other sites More sharing options...
.josh Posted March 12, 2009 Share Posted March 12, 2009 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 [^>] Quote Link to comment https://forums.phpfreaks.com/topic/149136-get-meta-description-tag-contentalmost-therei-think/#findComment-783217 Share on other sites More sharing options...
alwoodman Posted March 13, 2009 Author Share Posted March 13, 2009 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 & reviews about stuff</TITLE> thanks Lee Quote Link to comment https://forums.phpfreaks.com/topic/149136-get-meta-description-tag-contentalmost-therei-think/#findComment-783902 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.