2012-10-18 4 views
4

h1 태그 다음에 HTML 문자열을 넣고 다음 h1 태그까지 넣으려고합니다. 그런 다음 계속하십시오.XPath - 특정 태그 뒤에 모두 선택

예를 들어

, 여기에 HTML입니다 : XPath 쿼리가 될 것입니다 무엇

array(
    0 => '<p>Paragraph</p><ul><li>List item</li><li>List item</li></ul><p>Paragraph</p>', 
    1 => '<ul><li>List item</li><li>List item</li></ul><p>Paragraph<img /></p>' 
) 

h1 후 모든 콘텐츠를 선택 :
<h1>Heading</h1> 
<p>Paragraph</p> 
<ul> 
    <li>List item</li> 
    <li>List item</li> 
</ul> 
<p>Paragraph</p> 
<h1>Heading 2</h1> 
<ul> 
    <li>List item</li> 
    <li>List item</li> 
</ul> 
<p>Paragraph<img /></p> 

그리고이에서 나는이 배열을 만들려고하고

다음까지 태그 달기 등등?

도움이나 의견을 보내 주시면 감사하겠습니다.

UPDATE :

array(
    'headings' => array(
     1 => '<h1>Heading</h1>', 
     2 => '<h1>Heading 2</h1>' 
), 
    'content' => array(
     1 => '<p>Paragraph</p><ul><li>List item</li><li>List item</li></ul><p>Paragraph</p>', 
     2 => '<ul><li>List item</li><li>List item</li></ul><p>Paragraph<img /></p>' 
) 
) 
+0

나는이 깡통이 당신을 도와 생각에 http : // stackoverflow.com/questions/1 276753/xpath-select-first-element-after-some-other-element – cacoroto

답변

0

나는이 :)

$html = '<h1>Heading</h1><p>Paragraph</p><ul><li>List item</li><li>List item</li></ul><p>Paragraph</p><h1>Heading 2</h1><ul><li>List item</li><li>List item</li></ul><p>Paragraph<img /></p>'; 

$dom_document = new DOMDocument(); 

$dom_document->loadHTML($html); 
$dom_document->preserveWhiteSpace = false; 

//use DOMXpath to navigate the html with the DOM 
$dom_xpath = new DOMXpath($dom_document); 

$elements = $dom_xpath->query("/html/body/*"); 

if (!is_null($elements)) { 
    $i = 0; 
    foreach ($elements as $element) { 
     if ($element->nodeName == 'h1') { 
      $i++; 
      $array['headings'][$i] = $dom_document->saveHtml($element); 
      continue; 
     } else { 
      $array['content'][$i] .= $dom_document->saveHtml($element); 
     } 
    } 
} 

var_dump($array); 

처럼했다 :

내가 궁극적으로 PHP를 사용하는 것입니다 달성하기 위해 노력하고, 배열의 형식을 만들 참고 : PHP 5.2를 사용해야하는 경우 다음을 대체하십시오.

$array['headings'][$i] = $dom_document->saveHtml($element); 

$array['content'][$i] .= $dom_document->saveHtml($element); 

: 여기

$array['headings'][$i] = $dom_document->saveXml($element); 
$array['content'][$i] .= $dom_document->saveXml($element); 
1

는 할 수있는 빠른 방법입니다.

이 코드는 $code에 배치됩니다 가정하면

$code = <<<'CODE' 
<h1>Heading</h1> 
<p>Paragraph</p> 
<ul> 
    <li>List item</li> 
    <li>List item</li> 
</ul> 
<p>Paragraph</p> 
<h1>Heading 2</h1> 
<ul> 
    <li>List item</li> 
    <li>List item</li> 
</ul> 
<p>Paragraph<img /></p> 
CODE; 

솔루션 :

// Content array... 
$content = array_map(
    function ($element) { 
     return preg_replace('/\>\s+\</', '><', $element); 
    }, 
    preg_split('/\<h1\>[^\<]*\<\/h1\>/', $code) 
); 
array_shift($content); 

// Headings array... 
preg_match_all('/\<h1\>[^\<]*\<\/h1\>/', $code, $matches); 
$headings = $matches[0]; 

// Result 
$result = array(
    'headings' => $headings, 
    'content' => $content, 
); 
print_r($result); 

출력 :

Array 
(
    [headings] => Array 
     (
      [0] => <h1>Heading</h1> 
      [1] => <h1>Heading 2</h1> 
     ) 

    [content] => Array 
     (
      [0] => <p>Paragraph</p><ul><li>List item</li><li>List item</li></ul><p>Paragraph</p> 
      [1] => <ul><li>List item</li><li>List item</li></ul><p>Paragraph<img /></p> 
     ) 
) 
+0

고맙습니다. 내 정규식 기술 도움 : – beingalex