简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM