loadFromFile() публичный Метод

Loads the dom from a document file/url
public loadFromFile ( string $file, array $options = [] )
$file string
$options array
Пример #1
0
 private function getAndParseSubteInfo()
 {
     if ($cachedData = $this->isCached()) {
         $this->lineas = $cachedData;
         return true;
     }
     $dom = new Dom();
     $dom->loadFromFile($this->sourceURL);
     foreach ($this->lineas as $linea => $info) {
         $lineInfo = $dom->find("#status-line-{$linea}-container")[0];
         $lineStatusClass = $lineInfo->getAttribute('class');
         if (strpos($lineStatusClass, 'suspendido') !== false) {
             $this->lineas[$linea]->status = 'CANCELLED';
         }
         if (strpos($lineStatusClass, 'demorado') !== false) {
             $this->lineas[$linea]->status = 'DELAYED';
         }
         if ($this->isSleepingTime()) {
             $this->lineas[$linea]->status = 'SLEEPING';
         }
         // get raw status msg
         $status_msg = $lineInfo->find("#status-line-{$linea}")->text;
         $this->lineas[$linea]->statusMessage = html_entity_decode($status_msg, ENT_QUOTES, 'ISO-8859-1');
     }
     $this->updateStatusInfo();
     $this->cacheLines();
 }
Пример #2
0
 /**
  * Parses the given file and returns a Game object
  *
  * @param string $filename
  *
  * @return Game
  */
 protected function processFile($filename)
 {
     $this->command->out("Processing " . $filename);
     // Create a game object with home/away teams and other info
     $game = $this->createGameWithInfo($filename);
     $dom = new Dom();
     $dom->loadFromFile($filename);
     $lines = [];
     /** @var AbstractNode $tr */
     foreach ($dom->find('tr.evenColor') as $tr) {
         $lineContent = [];
         $lineCount = 0;
         /** @var AbstractNode $td */
         foreach ($tr->getChildren() as $td) {
             $value = $this->cleanUpLine($td->text);
             if ($value) {
                 $lineCount++;
                 // Each event is actually 6 lines
                 $lineContent[] = $value;
                 if ($lineCount % 6 == 0) {
                     $lines[] = $lineContent;
                     $lineContent = [];
                 }
             }
         }
     }
     // Add each event line to the game log
     foreach ($lines as $line) {
         if ($event = $this->createParsedEvent($line)) {
             $game->addEvent($event);
         }
     }
     return $game;
 }
Пример #3
0
 public function addPhoto($session, File $file, $number)
 {
     $path = $session . '.html';
     $update = $this->client->api('repo')->contents()->exists($this->user, $this->repo, $path, $this->branch);
     if ($update) {
         $set = new \PHPHtmlParser\Dom();
         $set->load($this->client->api('repo')->contents()->download($this->user, $this->repo, $path, $this->branch));
         $info = $this->client->api('repo')->contents()->show($this->user, $this->repo, $path, $this->branch);
     } else {
         $set = new \PHPHtmlParser\Dom();
         $set->loadFromFile($this->page);
     }
     $div = $set->find('#photos')[0];
     $photo = new \PHPHtmlParser\Dom();
     $photo->loadFromFile($this->photo);
     $img = $photo->find('img')[0];
     $img->setAttribute('src', $this->getPhotoFilename($file));
     $div->addChild($photo->root);
     $images = $div->find('img');
     $count = count($images);
     if ($count <= 4) {
         $meta = new Dom();
         $meta->load('<meta name="twitter:image' . --$count . '" content="' . $this->web . $this->getPhotoFilename($file, 'th') . '">');
         $set->find('head', 0)->addChild($meta->root);
     }
     $content = \Mihaeu\HtmlFormatter::format((string) $set);
     $content = preg_replace("#\n\\s*\n#", "\n", $content);
     if ($update) {
         $response = $this->client->api('repo')->contents()->update($this->user, $this->repo, $path, $content, 'Adding Photo ' . PHP_EOL . $file, $info['sha'], $this->branch);
     } else {
         $response = $this->client->api('repo')->contents()->create($this->user, $this->repo, $path, $content, 'Adding Page and Photo ' . PHP_EOL . $file, $this->branch);
         $this->addSession($session, $number, $file);
     }
 }
Пример #4
0
 public function testRemoveScriptsFalse()
 {
     $dom = new Dom();
     $dom->setOptions(['removeScripts' => false]);
     $dom->loadFromFile('tests/files/horrible.html');
     $this->assertEquals(1, count($dom->find('script')));
     $this->assertEquals('text/JavaScript', $dom->find('script')->getAttribute('type'));
 }
Пример #5
0
 /**
  * Creates a new dom object and calls loadFromFile() on the
  * new object.
  * @param string $file
  * @return $this
  */
 public static function loadFromFile($file)
 {
     $dom = new Dom();
     self::$dom = $dom;
     return $dom->loadFromFile($file);
 }
Пример #6
0
    public function testLoadFileBigTwicePreserveOption()
    {
        $dom = new Dom();
        $dom->loadFromFile('tests/files/big.html', ['preserveLineBreaks' => true]);
        $post = $dom->find('.post-row', 0);
        $this->assertEquals('<p>Журчанье воды<br />
Черно-белые тени<br />
Вновь на фонтане</p>', trim($post->find('.post-message', 0)->innerHtml));
    }
Пример #7
0
 public function testLoadFileBigTwice()
 {
     $dom = new Dom();
     $dom->loadFromFile('tests/files/big.html');
     $post = $dom->find('.post-row', 0);
     $this->assertEquals(' <p>Журчанье воды<br /> Черно-белые тени<br /> Вновь на фонтане</p> ', $post->find('.post-message', 0)->innerHtml);
 }
Пример #8
0
include "vendor/autoload.php";
use PHPHtmlParser\Dom;
?>
    <!doctype html>
    <html lang="ru">
    <head>
        <meta charset="UTF-8">
        <title>Таблица</title>
    </head>
    <body>
        <table>

<?php 
$dom = new Dom();
$dom->loadFromFile('index.html');
for ($i = 0; $i < 100; $i++) {
    $contents = $dom->find("tr td")[$i];
    if ($contents->text == "Адрес") {
        $i += 5;
        continue;
    }
    if (preg_match("|^[\\d]+\$|", $contents->text)) {
        echo "<tr><td>" . $contents->text . "</td></tr>";
    }
    echo "<br/>";
    echo $contents->text;
}
?>
        </table>
    </body>
Пример #9
0
 private function collectSites()
 {
     $old_data = json_decode(file_get_contents(Yii::getAlias('@runtime/data/chatapp.json')), true);
     $dom = new Dom();
     $dom->loadFromFile(Yii::getAlias('@runtime/sites.html'));
     $clinks = $blinks = [];
     $exclude = array_key_exists('exclude', $old_data) ? $old_data['exclude'] : ['auburn.craigslist.org', 'bham.craigslist.org', 'dothan.craigslist.org', 'shoals.craigslist.org', 'gadsden.craigslist.org', 'huntsville.craigslist.org'];
     /* @var Dom\AbstractNode $link */
     foreach ($dom->find('a') as $link) {
         $href = $link->getAttribute('href');
         if (0 !== strpos($href, '//')) {
             continue;
         }
         $href = str_replace('/', '', $href);
         if (in_array($href, $exclude, true)) {
             continue;
         }
         $clinks[] = $href;
     }
     shuffle($clinks);
     $dom->loadFromFile(Yii::getAlias('@runtime/backpage.html'));
     /* @var Dom\AbstractNode $link */
     foreach ($dom->find('a') as $link) {
         $href = $link->getAttribute('href');
         $href = str_replace(['http:', '/'], '', $href);
         if (in_array($href, $exclude, true)) {
             continue;
         }
         $blinks[] = $href;
     }
     shuffle($blinks);
     $data = ['total_count' => (int) $old_data['total_count'], 'current_site' => '', 'sites' => array_merge($clinks, $blinks), 'exclude' => $exclude];
     file_put_contents(Yii::getAlias('@runtime/data/chatapp.json'), json_encode($data));
 }