2016-05-30 2 views
0

自分のサービスページへの入力を自動化できるように、私は変換しようとしているパートナーのシステムステータスページを持っています。サービスのステータスは、<span>クラスによって提供されます。私はこれが理想から遠いことを知っていますが、これが更新を提供する唯一の方法です。私はこれがサービス状態であり、このよう<li class="status service3">はそれが何であるかのサービスを私に告げるよう<span class="arrow up">値を取得する必要がありhtmlタグをPHPの配列に変換する

<ul class="item-3"> 
    <li class="status service1"> 
     <span class="icon"></span> 
     <h6> 
     SERVICE 1 
     </h6> 
     <p> 
     Updated: 
     9:52 AM CST Apr, 24 
     </p> 
     <span class="arrow up"></span> 
    </li> 
    <li class="status service2"> 
     <span class="icon"></span> 
     <h6> 
     SERVICE 2 
     </h6> 
     <p> 
     Updated: 
     9:52 AM CST Apr, 24 
     </p> 
     <span class="arrow up"></span> 
    </li> 
    <li class="status service3"> 
     <span class="icon"></span> 
     <h6> 
     SERVICE 3 
     </h6> 
     <p> 
     Updated: 
     9:52 AM CST Apr, 24 
     </p> 
     <span class="arrow up"></span> 
    </li> 
</ul> 

:以下

は、HTMLのコピーです。

私のステータスページAPIは「アップ/ダウン」などではなくIDを使用しているため、ステータスとサービスを配列にする必要がありますので、それらを自分のフォーマットに変換できます。

+0

http://stackoverflow.com/questions/1592438/create-array-from-the-contents-of-div-tags-in -php –

+0

http://php.net/manual/en/function.preg-match-all.php –

答えて

0
$xml = new SimpleXMLElement($html); 

$result = $xml->xpath('//ul/li'); 
$up = array(); //array of servises that is 'up' 
$down = array(); //array of servises that is 'down' 
foreach ($result as $item) 
{ 
    if (strpos($item->span[1]->attributes()->class, 'up') !== false) 
    { 
     $up[] = str_replace("status ", "", $item->attributes()->class); 
    } 

    if (strpos($item->span[1]->attributes()->class, 'down') !== false) 
    { 
     $down[] = str_replace("status ", "", $item->attributes()->class); 
    } 
} 

var_dump($up); 
var_dump($down); 

"アップ" システムステータスのためのウィル出力

array(3) { 
    [0]=> 
    string(8) "service1" 
    [1]=> 
    string(8) "service2" 
    [2]=> 
    string(8) "service3" 
} 
+0

ありがとう@Dumkaaaしかし、ファイルからhtmlを読み込む方法を教えてください。 –

+0

@ red-spider単純な使用file_get_contents他のウェブサイトからのロード: $ html = file_get_contents( 'http://example.com'); ' またはあなたのサイトからinclude_path '// 5 $ file = file_get_contents( './ people.txt'、true); // PHP 5 $ file = file_get_contents( './ people.txt'、FILE_USE_INCLUDE_PATH); ' – Dumkaaa

+0

@Dumkaaaに感謝していますが、これは正しく動作するようになっているようです。上に示した配列の例ではなく、SimpleXMLElement全体を出力できます。 –

関連する問題