0

我有以下 HTML:

<div id="ABC">
    <i>Lorem Ipsum</i> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
    <br>
    It has survived not only <b>five centuries</b>, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with <i>desktop publishing software</i> like Aldus PageMaker including versions of Lorem Ipsum.
</div>

我正在使用以下查询将 ABC 内容存储在数组中:

foreach ( $xpath->query('//div[@id="ABC"]/text() | //div[@id="ABC"]/i | //div[@id="ABC"]/b') as $text ) {
     $data['content'][] = $text->nodeValue; 
}

输出是这样的:

   [content] => Array
        (
            [0] => Lorem Ipsum
            [1] => is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
            [2] => It has survived not only
            [3] => five centuries
            [4] => , but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with
            [5] => desktop publishing software
            [6] => like Aldus PageMaker including versions of Lorem Ipsum.
   )

如果我想要这样的输出有可能吗?

   [content] => Array
        (
            [0] => Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
            [1] => It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
   )
4

1 回答 1

0

您可以做的是在字符串中累积文本节点,直到遇到一个br节点。此时,您将累积的字符串添加到$data['content']数组中并将字符串重置为空。在循环结束时,如果它不为空,您还需要将累积的字符串添加到数组中。

所以循环应该是这样的:

$line = '';
foreach ( $xpath->query('//div[@id="ABC"]/text() | //div[@id="ABC"]/i | //div[@id="ABC"]/b  | //div[@id="ABC"]/br') as $text ) {
  if ($text->nodeName == 'br') {
    $data['content'][] = $line;
    $line = '';
  }
  else
    $line .= $text->nodeValue;
}
if ($line) $data['content'][] = $line;

请注意,我已在您的调用中添加了一个//div[@id="ABC"]/br查询,以便在循环中返回该节点。$xpath->querybr

于 2013-07-30T21:19:46.013 回答