• Edit
  • Delete

Dom selector in php

PHP Simple HTML DOM Parser

http://simplehtmldom.sourceforge.net/manual.htm

require_once sfConfig::get('sf_plugins_dir') .
'/ullCorePlugin/lib/vendor/SimpleHtmlDom/simple_html_dom.php';

$dom = str_get_html(
  $html,
  null,
  null,
  null,
  false // Preserve newlines
);

$elements = $dom->find('a');

foreach ($elements as &$element)
{
  var_dump($element->outertext); // <a href="http://www.ull.at>ull.at <b>Company</b></a>
  var_dump($element->innertext); // ull.at <b>Company</b>
  var_dump($element->plaintext); // ull.at Company
  var_dump($element->href);      // http://www.ull.at

  $element->outertext = '';      // delete tag
  $element->id = null;           // remove attribute
}

$html = $dom->__toString();



Other examples

Find all items with an id tag:

  • $elements = $dom->find('*[id]');

QueryPath

 

http://www.ibm.com/developerworks/opensource/library/os-php-querypath/index.html