簡體   English   中英

在PHP中使用// text()時,請包裝所有HTML標記:DOMXPath

[英]Wrap any HTML tags when using //text() in PHP: DOMXPath

我有以下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.
   )

您可以做的是將文本節點累積在字符串中,直到遇到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;

請注意,我已經在您的$xpath->query調用中添加了//div[@id="ABC"]/br查詢,以便在循環中返回br節點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM