Reputation: 1488
I'm wondering if I can use file get contents to get only specific elements within the desired page. For instance: only store the content within the <title></title>
tags or within the <meta></meta>
tags and so forth. If it's not possible with file_get_contents what can I use to achieve that?
Thanks!
Upvotes: 0
Views: 172
Reputation: 1860
You can use explode()
function;
$content = file_get_contents("http://www.demo.com");
$explodedContent = explode("<title>", $content);
$explodedExplodedContent = explode("</title>", $explodedContent[1]);
echo $explodedExplodedContent[0]; // title of that page.
You can make it into a function;
function contentBetween($beginStr, $endStr, $contentURL){
$content = file_get_contents($contentURL);
$explodedContent = explode($beginStr, $content);
$explodedExplodedContent = explode($endStr, $explodedContent[1]);
return $explodedExplodedContent[0];
}
echo contentBetween("<title>", "</title>", "http://www.demo.com"); // usage.
See more about explode()
f: http://php.net/manual/tr/function.explode.php
Upvotes: 1