soma56 Posted June 14, 2010 Share Posted June 14, 2010 Ok, so I've figured out how to parse and bring back content from within <title> and </title> tags. However I'm having a heck of a time trying to figure out how to grab the contents between <meta name="keywords" content="grab-this-content" /> My program seems to grab everything that ends with "/>". The keywords, in addition to several other elements that I don't need. The only thing I want is between: <meta name="keywords" content=" and the first instance of /> Does anyone understand what I'm trying to do? Can you please give me some direction? Link to comment https://forums.phpfreaks.com/topic/204686-only-return-the-first-instance-of-x/ Share on other sites More sharing options...
Catfish Posted June 14, 2010 Share Posted June 14, 2010 preg_match will do what you want. http://au.php.net/manual/en/function.preg-match.php Link to comment https://forums.phpfreaks.com/topic/204686-only-return-the-first-instance-of-x/#findComment-1071634 Share on other sites More sharing options...
Alex Posted June 14, 2010 Share Posted June 14, 2010 ~<meta name="keywords" content="([^"]+)~ $str = '<meta name="keywords" content="grab-this-content" />'; preg_match('~<meta name="keywords" content="([^"]+)~', $str, $matches); print_r($matches); Link to comment https://forums.phpfreaks.com/topic/204686-only-return-the-first-instance-of-x/#findComment-1071636 Share on other sites More sharing options...
ToonMariner Posted June 14, 2010 Share Posted June 14, 2010 Actually - in this case you may find using the DOM and XPath a better method - preg_match is expensive and slow... something like: $doc = new DOMDocument(); $doc->loadHTMLFile("/path/to/your/file.html"); $xpath = new DOMXpath($doc); then you can grab all manner of info without having to run preg_match over and over again.. $title = $xpath->query("//title")->item( 0 )->nodeValue; $keywords = $xpath->query("//meta[@name='keywords']")->item(0)->getAttribute('content'); and so on and so forth... You should find this method a bit more efficient/quicker and a bit more fun Link to comment https://forums.phpfreaks.com/topic/204686-only-return-the-first-instance-of-x/#findComment-1071650 Share on other sites More sharing options...
soma56 Posted June 15, 2010 Author Share Posted June 15, 2010 Just a quick thank you for everyone's response. I appreciate it. Link to comment https://forums.phpfreaks.com/topic/204686-only-return-the-first-instance-of-x/#findComment-1072176 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.