简体   繁体   中英

How do I get contents of a div class outside my website

I wanted to get the content of some div classes and echo it back to my website.

This is the example div of a website I'm trying to get

<div class="entry-content content">


Hi Goodmorning
<span class="cp-load-after-post"></span>  

</div>

I've already tried some of the codes but non of them worked.

$url = 'https://thewebsiteimtryingtogetcontent.com';
$content = file_get_contents($url);
$first_step = explode( '<div id="iwanttogetthisid">' , $content );
$second_step = explode("</div>" , $first_step[1] );

But then when I try this nothing echoes back, please help I'm new to php.

The DomCrawler component eases DOM navigation for HTML and XML documents.

https://symfony.com/doc/current/components/dom_crawler.html

composer require symfony/dom-crawler
require __DIR__.'/vendor/autoload.php';

use Symfony\Component\DomCrawler\Crawler;

$html = <<<'HTML'
<div class="content">


Hi Goodmorning
<span class="cp-load-after-post"></span>  

</div>
HTML;

$crawler = new Crawler($html);

echo $message = $crawler->filterXPath('//div[@class="content"]')->text();

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