There is such a html code:

<div class="fullnews"> <div class="fullnews-header"><a href="#">Some Text</a></div><br /> <div class="fullnews-header">Post Title</div> </div> 

You need to parse the 2nd block fullnews-header , skipping the first fullnews-header in which the links are located. How to do it? Laravel / PHP. The ready parser is already there, it only enters the first block fullnews-header ...

  • via css you can reach it like this: .fullnews .fullnews-header:nth-of-type(2) - can help and parse - lexxl
  • I tried: 1 - .fullnews .fullnews-header: nth-of-type (2) = ErrorException ... (it skips and takes the whole .fullnews block and there is a lot of text. There are plenty of text. Tobish is not working 2 - .fullnews-header: nth-of-type (2) = ErrorException (E_NOTICE) Array to string conversion. I can’t go to other site files unless I ask the owner to change this block = / - BonBonSlick
  • > The finished parser is already there, it only enters the first block of the fullnews-header ... Show what you have, it will be easier. - Alexey Ukolov
  • I have to jump over the first block. How to do this Laravel 4 / PhP 5.4 - BonBonSlick

1 answer 1

Laravel seems to have built-in tools from symfony components, but with standard tools you can do it like this:

 $html = '<div class="fullnews"> <div class="fullnews-header"><a href="#">Some Text</a></div><br /> <div class="fullnews-header">Post Title</div> </div>'; $dom = new DOMDocument(); $dom->loadHTML($html); $xpath = new DOMXPath($dom); $container = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' fullnews ')]")[0]; $node = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' fullnews-header ')]", $container)[1]; var_dump($node->textContent); 

http://sandbox.onlinephpfunctions.com/code/a895ca8f4929f3c69f7dacbb17530a159f2e077b

  • Works with a bang, only with Phn 5.6.1+, Help please integrate this into Laravel? PhP 5.4 + Laravel 4. Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN) Cannot use object of type DOMNodeList as array - BonBonSlick
  • For older versions of php, you just need to make a selection by the array index in a separate step: $ containerNodes = $ xpath-> query ("// * contains (concat ('', normalize-space (@class), ''), 'fullnews ')] "); $ container = $ containerNodes [0]; - Alexey Ukolov
  • Thanks, help. - BonBonSlick