Atlante Avila
Atlante Avila

Reputation: 1488

PHP File get contents

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

Answers (1)

Burak Tokak
Burak Tokak

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

Related Questions