开发者

PHP way of parsing HTML string

开发者 https://www.devze.com 2023-03-07 23:39 出处:网络
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>. I

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';


$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


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]');  


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">'
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号