本文整理汇总了PHP中domDocument::getElementsByTagname方法的典型用法代码示例。如果您正苦于以下问题:PHP domDocument::getElementsByTagname方法的具体用法?PHP domDocument::getElementsByTagname怎么用?PHP domDocument::getElementsByTagname使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类domDocument
的用法示例。
在下文中一共展示了domDocument::getElementsByTagname方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getSelectedItems
/**
* Extract selected items array from html multiselect tag
*
* @param array Html multiselect tag
* @return array Selected items array
*/
private function getSelectedItems($html)
{
$dom = new domDocument();
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$options = $dom->getElementsByTagname('option');
$items = array();
foreach ($options as $option) {
if ($option->hasAttribute('selected')) {
$items[] = $option->nodeValue;
}
}
return $items;
}
示例2: getTextBetweenTags
/**
*
* from http://www.phpro.org/examples/Get-Text-Between-Tags.html
*
* @get text between tags
* @param string $tag The tag name
* @param string $html The XML or XHTML string
* @param int $strict Whether to use strict mode
* @return array
*
*/
function getTextBetweenTags($tag, $html, $strict = 0)
{
$dom = new domDocument();
if ($strict == 1) {
$dom->loadXML($html);
} else {
$dom->loadHTML($html);
}
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagname($tag);
$out = array();
foreach ($content as $item) {
$out[] = $item->nodeValue;
}
return $out;
}
示例3: formatCode
protected function formatCode($article)
{
echo $cont = $article['content'];
$dom = new \domDocument();
$dom->loadHTML($cont);
$dom->preserveWhiteSpace = false;
$codeelement = $dom->getElementsByTagname('code');
$i = $codeelement->length - 1;
while ($i > -1) {
$element = $codeelement->item($i);
echo $element->nodeValue;
echo "<br />";
echo $newelement = htmlentities($element->nodeValue);
exit;
$element->parentNode->replaceChild($newelement, $element);
$i--;
}
exit;
return $article;
}
示例4: getTextBetweenTags
function getTextBetweenTags($tag, $html, $strict = 0)
{
/*** a new dom object ***/
//$dom = new domDocument;
$dom = new domDocument('1.0', 'UTF-8');
/*** load the html into the object ***/
$html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
@$dom->loadHTML($html);
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
/*** the tag by its tag name ***/
$content = $dom->getElementsByTagname($tag);
/*** the array to return ***/
////$out = array();
$out = "";
foreach ($content as $item) {
/*** add node value to the out array ***/
$out .= "\"" . $item->nodeValue . "\", ";
}
/*** return the results ***/
return $out;
}
示例5: getTextBetweenTags
/**
*
* @get text between tags
*
* @param string $tag The tag name
*
* @param string $html The XML or XHTML string
*
* @param int $strict Whether to use strict mode
*
* @return array
*
*/
function getTextBetweenTags($tag, $html, $strict = 0)
{
/*** a new dom object ***/
$dom = new domDocument();
/*** load the html into the object ***/
if ($strict == 1) {
$dom->loadXML($html);
} else {
$dom->loadHTML($html);
}
/*** discard white space ***/
$dom->preserveWhiteSpace = false;
/*** the tag by its tag name ***/
$content = $dom->getElementsByTagname($tag);
/*** the array to return ***/
$out = array();
foreach ($content as $item) {
/*** add node value to the out array ***/
$out[] = $item->nodeValue;
}
/*** return the results ***/
return $out;
}
示例6: domElement
echo "--------- Remove Attribute Node\n";
$attr = $rootnode->removeAttribute("src");
print "Removed " . $attr . " attributes.\n";
echo "--------- attributes of rootnode\n";
$attrs = $rootnode->attributes;
print_node_list($attrs);
echo "--------- children of an attribute\n";
$children = $attrs->item(0)->childNodes;
print_node_list($children);
echo "--------- Add child to root\n";
$myelement = new domElement("Silly", "Symphony");
$newchild = $rootnode->appendChild($myelement);
print_node($newchild);
print $dom->saveXML();
print "\n";
echo "--------- Find element by tagname\n";
echo " Using dom\n";
$children = $dom->getElementsByTagname("Silly");
print_node_list($children);
echo " Using elem\n";
$children = $rootnode->getElementsByTagName("Silly");
print_node_list($children);
echo "--------- Unlink Node\n";
print_node($children->item(0));
$rootnode->removeChild($children->item(0));
print_node_list($rootnode->childNodes);
print $dom->savexml();
echo "--------- Find element by id\n";
print "Not implemented\n";
echo "--------- Check various node_name return values\n";
print "Not needed\n";
示例7: uncode_get_back_html
//.........这里部分代码省略.........
$back_url = isset($json_data['thumbnail_url']) ? 'background-image: url(' . esc_url($json_data['thumbnail_url']) . ');' : '';
$background_mime = 'image';
break;
case 'oembed/vimeo':
case 'oembed/youtube':
$back_metavalues = unserialize($back_attributes->metadata);
$video_orig_w = $back_metavalues['width'];
$video_orig_h = $back_metavalues['height'];
$video_ratio = $video_orig_h === 0 ? 1.777 : $video_orig_w / $video_orig_h;
$header_background_video = ' data-ratio="' . $video_ratio . '" data-provider="' . ($background_mime === 'oembed/vimeo' ? 'vimeo' : 'youtube') . '" data-video="' . $back_attributes->guid . '" data-id="' . rand(10000, 99999) . '"';
$back_mime_css = ' video uncode-video-container';
break;
case 'oembed/soundcloud':
$url = $back_attributes->guid;
$accent_color = $front_background_colors['accent'];
$accent_color = str_replace('#', '', $accent_color);
$getValues = wp_remote_fopen('http://soundcloud.com/oembed?format=js&url=' . $url . '&iframe=true');
$decodeiFrame = substr($getValues, 1, -2);
$decodeiFrame = json_decode($decodeiFrame);
preg_match('/src="([^"]+)"/', $decodeiFrame->html, $iframe_src);
$iframe_url = str_replace('visual=true', 'visual=false', $iframe_src[1]);
$content_html = '<iframe width="100%" scrolling="no" frameborder="no" src="' . $iframe_url . '&color=' . $accent_color . '&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false"></iframe>';
break;
case 'oembed/twitter':
$url = 'https://api.twitter.com/1/statuses/oembed.json?id=' . basename($back_attributes->guid);
$json = wp_remote_fopen($url);
$json_data = json_decode($json, true);
$id = basename($json_data['url']);
$html = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $json_data['html']);
$html = str_replace("— ", '', $html);
$dom = new domDocument();
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$twitter_content = $dom->getElementsByTagname('blockquote');
$twitter_blockquote = '';
$twitter_footer = '';
foreach ($twitter_content as $item) {
$twitter_content_inner = $item->getElementsByTagname('p');
foreach ($twitter_content_inner as $item_inner) {
foreach ($item_inner->childNodes as $child) {
$twitter_blockquote .= $child->ownerDocument->saveXML($child);
}
$item_inner->parentNode->removeChild($item_inner);
}
foreach ($item->childNodes as $child) {
$twitter_footer .= $child->ownerDocument->saveXML($child);
}
$item->parentNode->removeChild($item);
}
$content_html = '<div class="twitter-item">
<div class="twitter-item-data">
<blockquote class="tweet-text pullquote">
<p>' . $twitter_blockquote . '</p>';
$content_html .= '<p class="twitter-footer"><small>' . $twitter_footer . '</small></p>';
$content_html .= '</blockquote>
</div>
</div>';
$poster = get_post_meta($background['background-image'], "_uncode_poster_image", true);
if ($poster !== '') {
$poster_attributes = uncode_get_media_info($poster);
$media_metavalues = unserialize($poster_attributes->metadata);
$image_orig_w = $media_metavalues['width'];
$image_orig_h = $media_metavalues['height'];
$resized_image = uncode_resize_image($poster_attributes->guid, $poster_attributes->path, $image_orig_w, $image_orig_h, 12, '', false);
$poster_url = $resized_image['url'];
if (isset($poster_attributes->post_mime_type)) {
示例8: getTextBetweenTags
/**
* Returns the text between 2 tags
*
* @return array
* @param string $tag The tag.
* @param string $html The HTML to search in.
* @param bool[optional] $strict Use strictmode?
*/
private function getTextBetweenTags($tag, $html, $strict = false)
{
// new dom document
$dom = new domDocument();
// load HTML
$strict == true ? $dom->loadXML($html) : $dom->loadHTML($html);
// discard whitespace
$dom->preserveWhiteSpace = false;
// the array with results
$results = array();
// fetch the tag by name
$content = $dom->getElementsByTagname($tag);
// loop the content
foreach ($content as $item) {
// add node value to results
$results[] = $item->nodeValue;
}
// return the results
return $results;
}
示例9: uncode_get_oembed
//.........这里部分代码省略.........
$media_oembed = '<iframe width="100%" scrolling="no" frameborder="no" src="' . $iframe_url . '&color=' . $accent_color . '&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false"></iframe>';
if (strpos($iframe_url, 'playlist') !== false) {
$object_class = 'soundcloud-playlist';
} else {
$object_class = 'soundcloud-single';
}
} else {
$media_oembed = '<img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=media+not+available&w=500&h=500" />';
}
}
break;
case 'oembed/spotify':
if (isset($poster) && $poster !== '' && $with_poster || $lighbox_code) {
$get_url = parse_url($url);
$break_spotify = explode('/', $get_url['path']);
$media_oembed = 'https://embed.spotify.com/?uri=spotify' . implode(':', $break_spotify);
} else {
$media_oembed = wp_oembed_get($url);
$media_oembed = preg_replace('#\\s(width)="([^"]+)"#', '', $media_oembed);
$media_oembed = preg_replace('#\\s(height)="([^"]+)"#', '', $media_oembed);
$object_class = 'object-size spotify';
}
break;
case 'oembed/twitter':
$url = 'https://api.twitter.com/1/statuses/oembed.json?id=' . basename($url);
$json = wp_remote_fopen($url);
$json_data = json_decode($json, true);
$id = basename($json_data['url']);
$html = preg_replace('#<script(.*?)>(.*?)</script>#is', '', $json_data['html']);
$html = str_replace("— ", '', $html);
$dom = new domDocument();
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$twitter_content = $dom->getElementsByTagname('blockquote');
$twitter_blockquote = '';
$twitter_footer = '';
foreach ($twitter_content as $item) {
$twitter_content_inner = $item->getElementsByTagname('p');
foreach ($twitter_content_inner as $item_inner) {
foreach ($item_inner->childNodes as $child) {
$twitter_blockquote .= $child->ownerDocument->saveXML($child);
}
$item_inner->parentNode->removeChild($item_inner);
}
foreach ($item->childNodes as $child) {
$twitter_footer .= $child->ownerDocument->saveXML($child);
}
$item->parentNode->removeChild($item);
}
$media_oembed = '<div class="twitter-item">
<div class="twitter-item-data">
<blockquote class="tweet-text pullquote">
<p>' . $twitter_blockquote . '</p>';
$media_oembed .= '<p class="twitter-footer"><i class="fa fa-twitter"></i><small>' . $twitter_footer . '</small></p>';
$media_oembed .= '</blockquote>
</div>
</div>';
$width = 1;
$height = 0;
$object_class = 'tweet object-size regular-text';
break;
case 'oembed/html':
$author = $author_img = '';
$width = 1;
$height = 0;
$poster = get_post_meta($id, "_uncode_poster_image", true);
示例10: getTextBetweenTags
/**
*
* @get text between tags
* @param string $tag The tag name
* @param string $html The XML or XHTML string
* @param int $strict Whether to use strict mode
* @param string $encoding
* @return array
*/
private function getTextBetweenTags($tag, $html, $strict = 0, $encoding = "UTF-8")
{
global $PAGE, $CFG;
if (!isset($CFG->filter_jsxgraph_divid)) {
set_config('filter_jsxgraph_divid', 'box');
}
if (!isset($CFG->filter_jsxgraph_boardvar)) {
set_config('filter_jsxgraph_boardvar', 'board');
}
if (!isset($CFG->filter_jsxgraph_width)) {
set_config('filter_jsxgraph_width', '500');
}
if (!isset($CFG->filter_jsxgraph_height)) {
set_config('filter_jsxgraph_height', '400');
}
// a new dom object
$dom = new domDocument();
$dom->formatOutput = true;
// load the html into the object
if ($strict == 1) {
$dom->loadXML($html);
} else {
libxml_use_internal_errors(true);
$htmlutf8 = mb_convert_encoding($html, 'HTML-ENTITIES', $encoding);
$dom->loadHTML($htmlutf8);
libxml_use_internal_errors(false);
}
// discard white space
$dom->preserveWhiteSpace = false;
$dom->strictErrorChecking = false;
$dom->recover = true;
// the tag by its tag name
$content = $dom->getElementsByTagname($tag);
if (count($content) > 0) {
$PAGE->requires->js(new moodle_url($CFG->wwwroot . '/filter/jsxgraph/jsxgraphcore.js'));
}
// Iterate backwards through the jsxgraph tags
$i = $content->length - 1;
while ($i > -1) {
$item = $content->item($i);
// Read attributes
$w = $item->getAttribute('width');
if ($w == "") {
$w = $CFG->filter_jsxgraph_width;
}
$h = $item->getAttribute('height');
if ($h == "") {
$h = $CFG->filter_jsxgraph_height;
}
$b = $item->getAttribute('box');
if ($b == "") {
$b = $CFG->filter_jsxgraph_divid . $i;
}
$brd = $item->getAttribute('board');
if ($brd == "") {
$brd = $CFG->filter_jsxgraph_boardvar . $i;
}
/* Create new div element containing JSXGraph */
$out = $dom->createElement('div');
$a = $dom->createAttribute('id');
$a->value = $b;
$out->appendChild($a);
$a = $dom->createAttribute('class');
$a->value = "jxgbox";
$out->appendChild($a);
$a = $dom->createAttribute('style');
$a->value = "width:" . $w . "px; height:" . $h . "px; ";
$out->appendChild($a);
$t = $dom->createTextNode("");
$out->appendChild($t);
$out = $dom->appendChild($out);
// Replace <jsxgraph> by <div>
$item->parentNode->replaceChild($out, $item);
$code = "";
$needGXT = false;
$url = $item->getAttribute('file');
if ($url != "") {
$code = "var " . $brd . " = JXG.JSXGraph.loadBoardFromFile('" . $b . "', '" . $url . "', 'Geonext');";
$needGXT = true;
} else {
$url = $item->getAttribute('filestring');
if ($url != "") {
$code = "var " . $brd . " = JXG.JSXGraph.loadBoardFromString('" . $b . "', '" . $url . "', 'Geonext');";
$needGXT = true;
} else {
// Plain JavaScript code
$code = $item->nodeValue;
}
}
// Place JavaScript code at the end of the page.
$PAGE->requires->js_init_call($code);
//.........这里部分代码省略.........
示例11: tagwrap
function tagwrap($html, $width = 75, $break = "nr")
{
$html = '<div>' . $html . '</div>';
//using dom object
$dom = new domDocument();
//load the html into the object
$dom->loadXML($html);
// preserve white space
$dom->preserveWhiteSpace = true;
//getting all tags
$content = $dom->getElementsByTagname("*");
$html = "";
foreach ($content as $item) {
//wrapping contents of tags, function described above is used,
//but you can use your own function or simple wordwrap() php function
$item->nodeValue = mb_wordwrap($item->nodeValue, $width, $break);
$html .= $dom->saveXML($item);
}
//return the results
return $html;
//html_entity_decode($html);
}