我知道我们可以使用PHP DOM来使用PHP解析HTML。我也在堆栈溢出中发现了很多问题。但是我有一个特定的要求。我有如下的HTML内容
<p class="Heading1-P"> <span class="Heading1-H">Chapter 1</span> </p> <p class="Normal-P"> <span class="Normal-H">This is chapter 1</span> </p> <p class="Heading1-P"> <span class="Heading1-H">Chapter 2</span> </p> <p class="Normal-P"> <span class="Normal-H">This is chapter 2</span> </p> <p class="Heading1-P"> <span class="Heading1-H">Chapter 3</span> </p> <p class="Normal-P"> <span class="Normal-H">This is chapter 3</span> </p>
我想解析以上HTML并将内容保存到两个不同的数组中,例如:
$heading 和 $content
$heading
$content
$heading = array('Chapter 1','Chapter 2','Chapter 3'); $content = array('This is chapter 1','This is chapter 2','This is chapter 3');
我可以简单地使用jQuery来实现。但是我不确定这是否正确。如果有人能指出我正确的方向,那就太好了。提前致谢。
尝试查看PHP简单HTML DOM解析器
它具有类似于jQuery的出色语法,因此您可以轻松按ID或类选择所需的任何元素
// include/require the simple html dom parser file $html_string = ' <p class="Heading1-P"> <span class="Heading1-H">Chapter 1</span> </p> <p class="Normal-P"> <span class="Normal-H">This is chapter 1</span> </p> <p class="Heading1-P"> <span class="Heading1-H">Chapter 2</span> </p> <p class="Normal-P"> <span class="Normal-H">This is chapter 2</span> </p> <p class="Heading1-P"> <span class="Heading1-H">Chapter 3</span> </p> <p class="Normal-P"> <span class="Normal-H">This is chapter 3</span> </p>'; $html = str_get_html($html_string); foreach($html->find('span') as $element) { if ($element->class === 'Heading1-H') { $heading[] = $element->innertext; }else if($element->class === 'Normal-H') { $content[] = $element->innertext; } }