PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 12, 2022

[FIXED] Get raw HTML code of element with Symfony DomCrawler

 March 12, 2022     goutte, php, symfony     No comments   

Issue

Html structure:

   <div id="product">
     <p>some text</p>
     <p>some text2</p>
   </div>    

My PHP code:

$client = new Client();
$crawler = $client->request('GET', $url);
echo $crawler->filter('#product')->text();

returns:

some text some text2

But I need:

<p>some text</p>
<p>some text2</p>

Solution

Well, there is one but ugly way - via iterating over its nodes:

$html = '';

foreach ($crawler as $domElement) {
    $html.= $domElement->ownerDocument->saveHTML();
}

Or, in your case, you should iterate over filtered element:

$html = '';     
$product = $crawler->filter('#produkt');
 
foreach ($product as $domElement) {
    foreach($domElement->childNodes as $node) {
        $html .= $domElement->ownerDocument->saveHTML($node);
    }
}

From documentation



Answered By - Vitalii Zurian
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing