Basit Html dom parser kullanarak html ayrıştırma

3 Cevap php

Ben bazı html ayrıştırmak için basit bir html dom parser kullanıyorum.

Ben böyle bir html var

<span class="UIStory_Message">
    Yeah, elixir of life!<br/>
   <a href="asdfasdf">
      <span>asdfsdfasdfsdf</span>
       <wbr/>
       <span class="word_break"/>
       61193133389&ref=nf
   </a>
</span>

Benim kod

$storyMessageNodes    = $story->find('span.UIStory_Message');
$storyMessage         = strip_tags($storyMessageNodest->innertext);

Ben doğru yayılma "UIStory_Message" içindeki metni almak istiyorum. yani, "Evet, hayatımın iksiri!".

but the above code gives me the whole text which is inside the whole span. ie, "Yeah, elixir of life! asdfsdfasdfsdf 61193133389&ref=nf "

nasıl ben sadece verir, böylece kod olabilir "Evet, hayatımın iksiri!" ?

3 Cevap

Ben zorlama DOM düğümler gereksiz unsurlardan kurtulmak için bir yöntem yazdım, ben yazar temas ettik, ama basit dom iki yıl boyunca aktif olmamıştır bu yüzden o dağıtımında da yer alacak şüpheliyim. İşte:

/**
 * remove specified nodes from selected dom
 *
 * @param string $selector
 * @param int|array (optional) possible values include:
 *   + positive integer - remove first denoted number of elements
 *   + negative integer - remove last denoted number of elements
 *   + array of ones and zeroes - remove the respective matches that equal to one
 *
 * eg.
 *   // will remove first two images found in node
 *   $dom->removeNodes('img',2);
 *
 *   // will remove last two images found in node
 *   $dom->removeNodes('img',-2);
 *
 *   // will remove all but the third images found in node
 *   $dom->removeNodes('img',array(1,1,0,1));
 *
 * [!!!] if there are more matches found than elements in array, the last array member will be used for processing
 *
 * eg.
 *   // will remove second and every following image
 *   $dom->removeNodes('img',array(0,1));
 *
 *   // will remove only the second image
 *   $dom->removeNodes('img',array(0,1,0));
 *
 * @return simple_html_dom_node
 */
public function removeNodes($selector, $limit = NULL)
{
    $elements = $this->find($selector);
    if ( empty($elements) ) return $this;


    if ( isset($limit) && is_int( $limit ) && $limit < 0 ) {
        $limit = abs( $limit );
        $elements = array_reverse( $elements );
    }

    foreach ( $elements as $element ) {

        if ( isset($limit) ) {

            if ( is_array( $limit ) ) {
                $current = current( $limit );
                if ( next( $limit ) === FALSE ) {
                    end( $limit );
                }
                if ( !$current ) {
                    continue;
                }
            } else {
                if ( --$limit === -1 ) {
                    return $this;
                }
            }
        }

        $element->outertext = '';

    }

    return $this;
}

simple_html_dom_node sınıfına veya 1 uzatmakla koydu. Soru soranlar durumda bu gibi kullanmak istiyorum:

$storyMessageNodes = $story->find('span.UIStory_Message');
$storyMessage = $storyMessageNodes[0]->removeNodes('a')->plaintext

Sen böyle bir şey yapabilirsiniz:

$result = $story->find('span.UIStory_Message');

Ve substr(), ilk < on; bir başka seçenek basit bir düzenli ifade yazmak için.


Ben test ettik, bu belgelere dayanarak sadece vahşi bir tahmin olduğunu, yapmaya çalışın:

$story->find('span.UIStory_Message')->plaintext; // same result as strip_tags()?

Veya:

$story->find('span.UIStory_Message')->find('text');

Bu işe yaramazsa, try playing with these options.

when you only delete the outer text you delete the HTML content itself, but if you perform another find on the same elements it will appear in the result. the reason is that the simple HTML DOM object still has it's internal structure of the element, only without its actual content. what you need to do in order to really delete the element is simply reload the HTML as string to the same variable. this way the object will be recreated without the deleted content, and the simple HTML DOM object will be built without it.

Burada bir örnek işlevi:

public function removeNode($selector)
{
    foreach ($html->find($selector) as $node)
    {
        $node->outertext = '';
    }

    $this->load($this->save());        
}

simple_html_dom sınıf içinde bu işlevi koymak ve sen iyisin.