7

I have a php string that contains the below HTML I am retrieving from an RSS feed. I am using simple pie and cant find any other way of splitting these two datasets it gets from <description>. If anyone knows of a way in simple pie to select children that would be great.

<div style="example"><div style="example"><img title="example" alt="example" src="example.jpg"/></div><div style="example">EXAMPLE TEXT</div></div>

to:

$image = '<img title="example" alt="example" src="example.jpg">';
$description = 'EXAMPLE TEXT';
ThomasReggi
  • 55,053
  • 85
  • 237
  • 424
  • http://stackoverflow.com/questions/7124823/file-get-html-displays-fatal-error-call-to-undefined-function – merrais Mar 10 '17 at 22:30

3 Answers3

9
$received_str = 'Your received html';

$html = str_get_html($received_str);

//Image tag
$img_tag = $html->find("img", 0)->outertext;

//Example Text
$example_text = $html->find('div[style=example]', 0)->last_child()->innertext;

See Here: http://simplehtmldom.sourceforge.net/manual.htm

Sadat
  • 3,493
  • 2
  • 30
  • 45
  • Just tested "Simple html DOM" and is not reliable on large files. In my case I've gor a file with 2800 divs inside and want to cycle among them but only first 21 occurrences were found.... – Power Engineering Jan 28 '20 at 16:35
3

Try Simple HTML Dom Parser

// Create DOM from HTML string
$html = str_get_html('Your HTML here');

// Find all images 
foreach($html->find('img') as $element) 
       echo $element->src . '<br>';

// Description
$description = $html->find('div[style=example]');  
Naveed
  • 41,517
  • 32
  • 98
  • 131
1

try using strip_tags:

<?php
    $html ='<div style="example"><div style="example"><img title="example" alt="example" src="example.jpg"/></div><div style="example">EXAMPLE TEXT</div></div>';
    $html = strip_tags($html,'<img>');
    // $html == '<img title="example" alt="example" src="example.jpg">'
?>
toshniba
  • 383
  • 5
  • 18
The Mask
  • 17,007
  • 37
  • 111
  • 185