6

我有一个 php 字符串,其中包含我从 RSS 提要检索的以下 HTML。我正在使用简单的饼图,找不到任何其他方法来拆分它从中获取的这两个数据集<description>。如果有人知道用简单的馅饼来选择孩子的方法,那就太好了。

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

3 回答 3

8
$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;

见这里:http ://simplehtmldom.sourceforge.net/manual.htm

于 2011-05-21T17:24:53.663 回答
2

尝试简单的 HTML Dom 解析器

// 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]');  
于 2011-05-21T16:44:40.287 回答
0

尝试使用 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">'
?>
于 2011-05-21T17:28:53.263 回答