本文整理汇总了PHP中DOMDocument::loadHtml方法的典型用法代码示例。如果您正苦于以下问题:PHP DOMDocument::loadHtml方法的具体用法?PHP DOMDocument::loadHtml怎么用?PHP DOMDocument::loadHtml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DOMDocument
的用法示例。
在下文中一共展示了DOMDocument::loadHtml方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: stdClass
function _testXPath($xpath_expression)
{
if (!class_exists('DOMDocument') || !class_exists('DOMXPath')) {
if (function_exists('domxml_open_mem')) {
$dom = domxml_open_mem($this->_response);
if (!$dom) {
$this->fail('Error parsing doc');
return false;
}
var_dump($dom);
$xpath = $dom->xpath_init();
var_dump($xpath);
$ctx = $dom->xpath_new_context();
var_dump($xpath_expression);
$result = $ctx->xpath_eval($xpath_expression);
var_dump($result);
$return = new stdClass();
$return->length = count($result->nodeset);
return $return;
}
$this->fail('No xpath support built in');
return false;
} else {
if (extension_loaded('domxml')) {
$this->fail('Please disable the domxml extension. Only php5 builtin domxml is supported');
return false;
}
}
$dom = new DOMDocument();
$dom->loadHtml($this->_response);
$xpath = new DOMXPath($dom);
$node = $xpath->query($xpath_expression);
return $node;
}
示例2: __construct
protected function __construct($url, $status, array $headers, $body)
{
parent::__construct($url, $status, $headers, $body);
$this->_domDocument = new DOMDocument();
$this->_domDocument->preserveWhiteSpace = true;
// We have to silence this out because invalid documents
// tend to throw allot of warnings
@$this->_domDocument->loadHtml($body);
}
示例3: loadHtml
/**
* @param string $html
* @return \DiDom\Element
* @throws \InvalidArgumentException
*/
public function loadHtml($html)
{
if (!is_string($html)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, is_object($html) ? get_class($html) : gettype($html)));
}
libxml_use_internal_errors(true);
libxml_disable_entity_loader(true);
$this->document->loadHtml($html);
libxml_clear_errors();
libxml_disable_entity_loader(false);
libxml_use_internal_errors(false);
return $this;
}
示例4: get_members
function get_members($url)
{
$html = get_html($url);
if ($html === false) {
echo 'connection error';
} else {
$oldSetting = libxml_use_internal_errors(true);
libxml_clear_errors();
$dom = new DOMDocument();
$dom->loadHtml($html);
$tbody = $dom->getElementsByTagName('tbody');
$trs = $tbody[0]->getElementsByTagName('tr');
global $parteinameFilter;
$members = array();
foreach ($trs as $tr) {
$tds = $tr->getElementsByTagName('td');
$link = $tds[0]->getElementsByTagName('a');
$member = array('name' => $link[0]->nodeValue, 'link' => $link[0]->getAttribute('href'), 'partei' => str_replace($parteinameFilter, '', $tds[1]->nodeValue));
$aze = str_replace(' ', '', htmlentities($tds[2]->nodeValue));
if ($aze) {
$member['amtszeitende'] = $aze;
}
$members[] = $member;
}
libxml_clear_errors();
libxml_use_internal_errors($oldSetting);
return $members;
}
return false;
}
示例5: get_commitees
function get_commitees($url)
{
$html = get_html($url);
if ($html === false) {
echo 'connection error';
} else {
$oldSetting = libxml_use_internal_errors(true);
libxml_clear_errors();
$dom = new DOMDocument();
$dom->loadHtml($html);
$tbody = $dom->getElementsByTagName('tbody');
$trs = $tbody[0]->getElementsByTagName('tr');
$commitees = array();
foreach ($trs as $tr) {
$tds = $tr->getElementsByTagName('td');
$link = $tds[0]->getElementsByTagName('a');
if ($link->length > 0) {
$commitee = array('name' => $link[0]->nodeValue, 'link' => $link[0]->getAttribute('href'));
}
$commitees[] = $commitee;
}
libxml_clear_errors();
libxml_use_internal_errors($oldSetting);
return $commitees;
}
return false;
}
示例6: fixChildrenAttribute
static function fixChildrenAttribute($elementType, $name, $value)
{
var_dump('AbstractElement\\Helper::fixChildrenAttribute needs fixed');
exit;
$classPath = '\\AbstractElement\\' . $elementType;
// each element in contents array for this object
foreach ($this->contents as $index => $content) {
// is this an object that extends AbstractElement?
if (is_a($content, '\\Element')) {
// is this of the right element type?
if (is_a($content, $classPath)) {
$content->setAttribute($name, $value);
}
$content->fixChildrenAttribute($elementType, $name, $value);
} elseif (is_string($content) && $fixRawHtml) {
$dom = new \DOMDocument();
$dom->loadHtml($content);
$reflectionClass = new \ReflectionClass($classPath);
$elements = $dom->getElementsByTagName($reflectionClass->getConstant('tag'));
foreach ($elements as $element) {
$element->setAttribute($name, $value);
}
$this->contents[$index] = $dom->saveHTML();
}
}
}
示例7: uploadTextarea
public static function uploadTextarea($texto, $tipo_midia)
{
$nomeTipo = TipoMidia::findOrFail($tipo_midia)->descricao;
// gravando imagem do corpo da noticia
$dom = new \DOMDocument();
$dom->loadHtml($texto, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$images = $dom->getElementsByTagName('img');
// foreach <img> in the submited message
foreach ($images as $img) {
$src = $img->getAttribute('src');
// if the img source is 'data-url'
if (preg_match('/data:image/', $src)) {
// get the mimetype
preg_match('/data:image\\/(?<mime>.*?)\\;/', $src, $groups);
$mimetype = $groups['mime'];
// Generating a random filename
$filename = md5(uniqid());
$filepath = "uploads/" . $nomeTipo . "/" . $filename . '.' . $mimetype;
// @see http://image.intervention.io/api/
$image = Image::make($src)->encode($mimetype, 100)->save(public_path($filepath));
$new_src = asset($filepath);
$img->removeAttribute('src');
$img->setAttribute('src', $new_src);
}
}
return $dom->saveHTML();
}
示例8: remove_link_tags
function remove_link_tags($content)
{
$old_xml_err = libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHtml(mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8"));
foreach ($dom->getElementsByTagName('link') as $link) {
$link->parentNode->removeChild($link);
}
$content_out = '';
$node = $dom->firstChild;
while ($node) {
$content_out .= $dom->saveHTML($node);
/* repeat for all nodes at this level */
$node = $node->nextSibling;
}
foreach (libxml_get_errors() as $error) {
/* just ignore warnings */
if ($error->level === LIBXML_ERR_WARNING) {
continue;
}
fof_log(__FUNCTION__ . ': ' . $error->message);
}
libxml_clear_errors();
libxml_use_internal_errors($old_xml_err);
return $content_out;
}
示例9: fetchConversationWith
/**
* Fetch conversation with
*
* @access public
* @var string $gamertag
* @var string $region
* @var string $sender
* @return array
*/
public function fetchConversationWith($gamertag, $region, $sender)
{
$gamertag = trim($gamertag);
$url = 'https://account.xbox.com/' . $region . '/Messages/UserConversation?senderGamerTag=' . $sender;
$key = $this->version . ':getMessages.' . $gamertag;
$data = $this->fetch_url($url);
$doc = new DOMDocument();
if (!empty($sender) && !empty($gamertag)) {
$doc->loadHtml($data);
$xpath = new DOMXPath($doc);
$postThumbLinks = $xpath->query("//div[@class='messageContent']");
$i = 0;
$array = array();
$last_sender = "";
foreach ($postThumbLinks as $link) {
$body = $this->find($link->ownerDocument->saveHTML($link), '<div class="messageBody">', '</div>');
$time = $this->find($link->ownerDocument->saveHTML($link), '<div class="sentDate localTime">', '</div>');
$sender = $this->find($link->ownerDocument->saveHTML($link), '<div class="senderGamertag">', '</div>');
$array[$i]['message'] = $body;
$array[$i]['time'] = $time;
if ($sender) {
$array[$i]['sender'] = $sender;
$last_sender = $sender;
} else {
$array[$i]['sender'] = $last_sender;
}
$i++;
}
} else {
return false;
}
return $array;
}
示例10: load
/**
* Load HTML or XML.
*
* @param string $string HTML or XML string or file path
* @param bool $isFile Indicates that in first parameter was passed to the file path
* @param string $type Type of document
* @param int $options Additional parameters
*/
public function load($string, $isFile = false, $type = 'html', $options = 0)
{
if (!is_string($string)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, is_object($string) ? get_class($string) : gettype($string)));
}
if (!in_array(strtolower($type), ['xml', 'html'])) {
throw new InvalidArgumentException(sprintf('Document type must be "xml" or "html", %s given', __METHOD__, is_object($type) ? get_class($type) : gettype($type)));
}
if (!is_integer($options)) {
throw new InvalidArgumentException(sprintf('%s expects parameter 4 to be integer, %s given', __METHOD__, is_object($options) ? get_class($options) : gettype($options)));
}
$string = trim($string);
if ($isFile) {
$string = $this->loadFile($string);
}
if (substr($string, 0, 5) !== '<?xml') {
$prolog = sprintf('<?xml version="1.0" encoding="%s"?>', $this->document->encoding);
$string = $prolog . $string;
}
$this->type = strtolower($type);
Errors::disable();
$this->type === 'xml' ? $this->document->loadXml($string, $options) : $this->document->loadHtml($string, $options);
Errors::restore();
return $this;
}
示例11: fix
public static function fix($html)
{
if (empty($html)) {
return $html;
}
$html = self::xss($html);
if (substr($html, 0, 2) !== '<p') {
$html = '<p>' . implode('</p><p>', preg_split('/[\\n\\r]/', $html)) . '</p>';
}
$html = trim(str_replace(["\n", "\r"], ' ', self::xss($html)));
$html = preg_replace('#\\s{2,}#', ' ', $html);
$valid = 'class|src|target|alt|title|href|rel';
$html = preg_replace('#<(font|span) style="font-weight[^"]+">([^<]+)</(font|span)>#i', '<strong>$2</strong>', $html);
$html = preg_replace('#<(font|span) style="font-style:\\s*italic[^"]+">([^<]+)</(font|span)>#i', '<i>$2</i>', $html);
$html = preg_replace('# (' . $valid . ')=#i', ' |$1|', $html);
$html = preg_replace('# [a-z]+=["\'][^"\']*["\']#i', '', $html);
$html = preg_replace('#\\|(' . $valid . ')\\|#i', ' $1=', $html);
$html = preg_replace('#</?(font|span)[^>]*>#', '', $html);
$html = preg_replace('#<(/?)div#', '<$1p', $html);
$html = preg_replace('#<(/?)b>#', '<$1strong>', $html);
$html = preg_replace('#<br\\s*/?>#', '</p><p>', $html);
libxml_use_internal_errors(true);
$DOM = new \DOMDocument();
$DOM->recover = true;
$DOM->preserveWhiteSpace = false;
$DOM->substituteEntities = false;
$DOM->loadHtml(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'), LIBXML_NOBLANKS | LIBXML_ERR_NONE);
$DOM->encoding = 'utf-8';
$html = $DOM->saveHTML();
libxml_use_internal_errors(false);
$html = preg_replace('~<(?:!DOCTYPE|/?(?:\\?xml|html|head|body))[^>]*>\\s*~i', '', $html);
$html = preg_replace('#<([^\\s]+)[^>]*>\\s*</\\1>#', '', $html);
$html = preg_replace('#</p>\\s+<p#', '</p><p', $html);
return trim(str_replace(' ', ' ', $html));
}
示例12: googleArray
/**
* Returns array, containing detailed results for any Google search.
*
* @access private
* @param string $query String, containing the search query.
* @param string $tld String, containing the desired Google top level domain.
* @return array Returns array, containing the keys 'URL', 'Title' and 'Description'.
*/
public static function googleArray($query)
{
$result = array();
$pages = 1;
$delay = 0;
for ($start = 0; $start < $pages; $start++) {
$url = 'http://www.google.' . GOOGLE_TLD . '/custom?q=' . $query . '&filter=0' . '&num=100' . ($start == 0 ? '' : '&start=' . $start . '00');
$str = SEOstats::cURL($url);
if (preg_match("#answer=86640#i", $str)) {
$e = 'Please read: http://www.google.com/support/websearch/' . 'bin/answer.py?&answer=86640&hl=en';
throw new SEOstatsException($e);
} else {
$html = new DOMDocument();
@$html->loadHtml($str);
$xpath = new DOMXPath($html);
$links = $xpath->query("//div[@class='g']//a");
$descs = $xpath->query("//td[@class='j']//div[@class='std']");
$i = 0;
foreach ($links as $link) {
if (!preg_match('#cache#si', $link->textContent) && !preg_match('#similar#si', $link->textContent)) {
$result[] = array('url' => $link->getAttribute('href'), 'title' => utf8_decode($link->textContent), 'descr' => utf8_decode($descs->item($i)->textContent));
$i++;
}
}
if (preg_match('#<div id="nn"><\\/div>#i', $str) || preg_match('#<div id=nn><\\/div>#i', $str)) {
$pages += 1;
$delay += 200000;
usleep($delay);
} else {
$pages -= 1;
}
}
}
return $result;
}
示例13: testRequestToOutputFile
function testRequestToOutputFile()
{
$client = new ProxyClient();
$client->URL = df_absolute_url('tests/test_ProxyClient/test1.html');
$outputFile = tempnam(sys_get_temp_dir(), 'test_ProxyClient');
$client->outputFile = $outputFile;
$client->process();
$this->assertEquals(null, $client->content, 'Content should be written to output file, not saved to variable.');
$expected = file_get_contents('tests/test_ProxyClient/test1.html');
$doc = new DOMDocument();
@$doc->loadHtml($expected);
$expected = $doc->saveHtml();
$actual = file_get_contents($outputFile);
$actual = '';
$fh = fopen($outputFile, 'r');
while (!feof($fh) and trim($line = fgets($fh, 1024))) {
// We skip the headers
}
ob_start();
fpassthru($fh);
fclose($fh);
$actual = ob_get_contents();
ob_end_clean();
unset($doc);
$doc = new DOMDocument();
@$doc->loadHtml($actual);
$actual = $doc->saveHtml();
unset($doc);
$this->assertEquals($expected, $actual);
}
示例14: it_should_remove_filtered_file_names
public function it_should_remove_filtered_file_names()
{
$domDoc = new \DOMDocument();
$domDoc->loadHtml('<html><body><img src="img/spacer.gif" /><img src="img/sprite.png" /><img src="img/cat.jpg" /></body></html>');
$document = UrlDocument::build($domDoc, 'http://simplegifts.co');
$analyzer = new StubFileSizeAnalyzer();
$this->beConstructedThrough('load', [$document, $analyzer]);
$this->process()->shouldHaveCount(1);
}
示例15: getPageMetrics
public function getPageMetrics($url = false)
{
$url = false != $url ? $url : self::getUrl();
$dataUrl = sprintf(services::OPENSITEEXPLORER_URL, 'links', '1', $url);
$html = HttpRequest::sendRequest($dataUrl);
$doc = new DOMDocument();
@$doc->loadHtml($html);
$data = $doc->getElementsByTagName('td');
return array('domainAuthority' => trim(strip_tags($data->item(0)->textContent)), 'pageAuthority' => trim(strip_tags($data->item(1)->textContent)), 'linkingRootDomains' => trim(strip_tags($data->item(2)->textContent)), 'totalInboundLinks' => trim(strip_tags($data->item(3)->textContent)));
}