Metin parçaları simplehtmldom dışarıdan etiketleri (inbetween etiketleri) yer ayrıştırmak nasıl?

2 Cevap php

I simplehtmldom html ayrıştırmak ve herhangi bir etiketi dışında bulunan şifresiz ayrıştırma sıkışmış (ancak iki farklı etiketleri arasında) dilerim için kullanıyorum:

<div class="text_small">
 <b>Аdress:</b> 7 Hange Road<br>    
 <b>Phone:</b> 415641587484<br>    
 <b>Contact:</b> Alex<br>    
 <b>Meeting Time:</b> 12:00-13:00<br>
</div>

Is it possible to get these values of Adress, Phone, Contact, Meeting Time? I wonder if there is a opportunity to pass CSS Selectors into nextSibling/previousSibling functions...

foreach($html->find('div.text_small') as $div_descr) 
 {
   foreach($div_descr->find('b') as $b) 
 {
 if ($b->innertext=="Аdress:") {//someaction
                }
 if ($b->innertext=="Phone:") { //someaction
                }
        if ($b->innertext=="Contact:") { //someaction
                }
        if ($b->innertext=="Meeting Time:") { //someaction
                }
    }
 }

Ne var onun yerine "someaction" kullanmalıyım?

gnc. Evet, hedef sayfayı düzenlemek için bir erişim yok. Aksi takdirde, bu değer olurdu? :)

2 Cevap

Çok daha basit bir çözüm olabilir. (Belki simple_html_dom daha başka bir şey kullanarak)

I haven't found a suitable selector and nextSibling() only returns the next sibling element. (Which is a bit strange. simple_html_dom_node stores two arrays, $children and $nodes. Textnodes are in $nodes but not in $children. And next_sibling() operates on $children).
But since $nodes is a public property of simple_html_dom_node you write some iterator yourself.

<?php
require_once 'simplehtmldom/simple_html_dom.php';
$html = str_get_html('<html><head><title>...</title></head><body>
  <div class="text_small">
    <b>Adress:</b> 9 Hange Road<br>    
    <b>Phone:</b> 999641587484<br>    
    <b>Contact:</b> Alex<br>    
    <b>Meeting Time:</b> 12:00-13:00<br>
  </div>
  <div class="text_small">
    <b>Adress:</b> 8 Hange Road<br>    
    <b>Phone:</b> 888641587484<br>    
    <b>Contact:</b> Bob<br>    
    <b>Meeting Time:</b> 13:00-14:00<br>
  </div>
</body></html>');

foreach($html->find('div.text_small') as $div) {
  $result = parseEntry($div);
  foreach($result as $r) {
    echo "'$r[name]' - '$r[text]'\n";
  }
  echo "========\n"; 
}

function parseEntry(simple_html_dom_node $div) {
  $result = array();
  $current = null;
  for($i=0; $i<count($div->nodes); $i++) {
    if ( HDOM_TYPE_ELEMENT===$div->nodes[$i]->nodetype) {
      if ( !is_null($current) ) {
        $result[] = $current;
        $current = null;
      }
      if ('b'===$div->nodes[$i]->tag) {
        $current = array('name'=>$div->nodes[$i]->text(), 'text'=>'');
      }
    }
    else if (HDOM_TYPE_TEXT===$div->nodes[$i]->nodetype && !is_null($current)) {
      $current['text'] .= $div->nodes[$i]->text();
    }
  }
  if ( !is_null($current) ) {
    $result[] = $current;
  }
  return $result;
}

baskılar

'Adress:' - ' 9 Hange Road'
'Phone:' - ' 999641587484'
'Contact:' - ' Alex'
'Meeting Time:' - ' 12:00-13:00'
========
'Adress:' - ' 8 Hange Road'
'Phone:' - ' 888641587484'
'Contact:' - ' Bob'
'Meeting Time:' - ' 13:00-14:00'
========

Başkası basit bir çözüm bulana kadar, bir başlangıç ​​noktası olarak kullanmak isteyebilirsiniz.

u etiketi içinde olmayan değerler span etiketi koyabilirsiniz. Belki u sonra halledebilirim

U Biraz tarz vermek kadar <span> değerlerine hiçbir şey yana