2013-07-30 2 views
0

У меня есть следующий HTML:Wrap любые HTML-теги при использовании // текст() в PHP: DOMXPath

<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. 
    ) 

ответ

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->query вызова, чтобы br узел возвращается в цикл.

+0

Вы мужчина @ james-держатель, поэтому прямой и простой ответ, как мне нужно :) –

Смежные вопросы