本文整理汇总了PHP中simple_html_dom::save方法的典型用法代码示例。如果您正苦于以下问题:PHP simple_html_dom::save方法的具体用法?PHP simple_html_dom::save怎么用?PHP simple_html_dom::save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类simple_html_dom
的用法示例。
在下文中一共展示了simple_html_dom::save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: parse
public function parse($isUpdate = false)
{
Ibos::import("application.extensions.simple_html_dom", true);
if ($isUpdate) {
$model = preg_replace("/\\s+data-id\\s?=\\s?\"?\\d+\"?/i", "", $this->printmodel);
$max = 0;
} else {
$model = $this->printmodel;
$max = intval($this->itemmax);
}
$elements = array();
$doc = new simple_html_dom();
$doc->load($model, true, true, CHARSET);
$items = $doc->find("ic");
$config = $this->getItemConfig();
if (!empty($items) && !empty($config)) {
$this->refactor($items, $config, $max, $elements);
}
$html = $doc->save();
$this->_cache = $elements;
CacheUtil::set("form_" . $this->ID, $elements);
$form["printmodelshort"] = $html;
if ($max != $this->itemmax) {
$form["itemmax"] = $max;
}
$doc->clear();
FlowFormType::model()->modify($this->ID, $form);
}
示例2: addToTable
function addToTable($text,$position,$button){
$dom = new simple_html_dom();
$dom->load($text);
$tableEl = $dom->find('.'.$position,0);
if(!$tableEl){
$table = '<table class="'.$position .' myApiShareTable"></table>';
$text = ($position == 'myApiShareTop') ? $table.$text : $text.$table;
$dom->load($text);
}
$text = $dom->save();
$dom->load($text);
$rowEl = $dom->find('.'.$position,0)->find('.myApiButtons',0);
if(!$rowEl){
$tr = '<tr class="myApiButtons"><td><table><tr><td>'.$button.'</td></tr></table></td></tr>';
$row = $dom->find('.'.$position,0);
$row->innertext = $tr.$row->innertext;
}else{
$rowEl->find('table',0)->find('tr',0)->innertext = '<td>'.$button.'</td>'.$rowEl->find('table',0)->find('tr',0)->innertext;
}
$text = $dom->save();
$dom->load($text);
$commentsTable = $dom->find('.myApiShareBottom',0);
if($commentsTable){
$commentsEl = $commentsTable->find('.myApiCommentsCell',0);
if($commentsEl){
$buttonRow = $commentsTable->find('.myApiButtons',0);
if($buttonRow){
$commentsEl->colspan = sizeof($buttonRow->find('td'));
$text = $dom->save();
}
}
}
$dom->clear(); unset($dom);
return $text;
}
示例3: buffer
function buffer(){
ob_start();
require(ROOT.DS.MAIN.DS.'reflex'.DS.'templates'.DS.$this->_template.'.php');
$html_string = ob_get_clean();
$html_string = str_replace('<php>','<?php ',$html_string);
$html_string = str_replace('</php>',' ?>',$html_string);
require(ROOT.DS.MAIN.DS.'library'.DS.'simple_html_dom.php');
$html = new simple_html_dom();
$html->load($html_string);
$b = $html->find('base',0);
if(!isset($b)){
$html->find('head',0)->innertext = '<base href="http://'.THIS_DOMAIN.'/"/>'.$html->find('head',0)->innertext;
$html_string = $html->save();
}
$fileloc = fopen(ROOT.DS.MAIN.DS.'reflex'.DS.'admin'.DS.'documents'.DS.'dont_touch_this_file.php', 'w');
fwrite($fileloc,$html_string);
fclose($fileloc);
}
示例4: foreach
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
<li>Tea</li>
<li>Milk</li>
</ul>
<li>Tea</li>
<li>Milk</li>
</ul>
</body>
</html>';
//creating DOM object
$domObject1 = new simple_html_dom($htmlText);
//dumping it to a string
$str = $domObject1->save();
echo $str;
//Printing nested elements
foreach ($domObject1->find('ul') as $ul) {
foreach ($ul->find('li') as $li) {
echo $li->plaintext . "\n";
}
}
echo "\n";
//Accesing direct descendant elements
$domObject1->find('ul ul ul li', 2)->plaintext = 'Not enough milk! :(';
echo $domObject1->find('ul')[0]->children()[0]->plaintext . "\n";
//prints 'Coffee'
echo $domObject1->find('ul', 0)->children(0)->plaintext . "\n";
//prints the same as the last line
echo $domObject1->find('ul', 2)->children(2)->plaintext . "\n";
示例5: inlineCss
function inlineCss($html)
{
// パースしやすいように無駄な空白や改行を取り除く
$html = preg_replace('!\\s+!', ' ', trim($html));
// headからCSSファイルを取り出す
preg_match('/<head>.*<\\/head>/', $html, $match);
$dom = new simple_html_dom();
$dom->load($match[0], true);
$css = '';
foreach ($dom->find('link[type=text/css]') as $e) {
if (is_object($e)) {
$url = 'http://' . env('HTTP_HOST') . $e->href;
$css .= file_get_contents($url);
}
}
$dom->clear();
// CSSのパース
$styles = $this->_parseCss($css);
// a:*をヘッダ内に格納
$css = '<style type="text/css"> <![CDATA[' . "\n";
$links = array('a:link', 'a:hover', 'a:focus', 'a:visited');
foreach ($links as $link) {
if (isset($styles[$link])) {
$css .= $link . '{' . $styles[$link] . '}' . "\n";
unset($styles[$link]);
}
}
$css .= ']]> </style>';
$html = preg_replace('/<\\/head>/', $css . ' </head>', $html);
// bodyを取り出す
preg_match('/<body[^>]*>.*<\\/body>/', $html, $match);
$dom = new simple_html_dom();
$dom->load($match[0], true);
// インライン化
foreach ($styles as $element => $style) {
$es = $dom->find($element);
foreach ($es as $e) {
if (is_object($e)) {
if (isset($e->attr['style'])) {
$style .= str_replace('"', '', $e->attr['style']);
}
$e->attr = array_merge($e->attr, array('style' => '"' . $style . '"'));
}
}
}
// session_idの付与
$targets = array('a', 'form');
foreach ($targets as $target) {
$es = $dom->find($target);
foreach ($es as $e) {
if ('a' === $target && isset($e->attr['href'])) {
$url = $e->attr['href'];
$e->attr['href'] = $this->_url($url);
}
if ('form' === $target && isset($e->attr['action'])) {
$url = $e->attr['action'];
$e->attr['action'] = $this->_url($url);
}
}
}
// html再構成
$body = $dom->save();
$dom->clear();
$html = preg_replace("/<body>.*<\\/body>/", $body, $html);
$html = preg_replace("/> /", ">\n", $html);
$html = preg_replace("/ </", "\n<", $html);
return $html;
}
示例6: flash_replace
function flash_replace($texte_a_formater)
{
$html = new simple_html_dom();
$html->load($texte_a_formater);
$flv_found = 0;
foreach ($html->find("object") as $element) {
// SE E' RELATIVO A UN FILE FLV
if (substr(strtoupper($element->data), -4) == ".FLV" || substr(strtoupper($element->data), -4) == ".MP4") {
$flv_found++;
$flv_file = $element->data;
$flv_width = $element->width;
$flv_height = $element->height;
$flv_type = $element->type;
$element->outertext = "";
$flv_code = "";
/* $flv_code .="<object type=\"".$flv_type."\" data=\"".$topdir.$v_Nom_Rep_Admin."/include/editeurs/ressources/player_flv_multi.swf\" width=\"".$flv_width."\" height=\"".$flv_height."\">
<param name=\"movie\" value=\"".$topdir.$v_Nom_Rep_Admin."/include/editeurs/ressources/player_flv_multi.swf\" />
<param name=\"allowFullScreen\" value=\"true\" />
<param name=\"FlashVars\" value=\"flv=".$flv_file."&showstop=0&showvolume=1&showtime=0&showopen=0&margin=0&autoplay=0&showiconplay=1&loop=0&volume=150&showfullscreen=1&buffer=10&ondoubleclick=fullscreen&buffermessage=&allowfullscreen=true\" />
</object>";*/
$flv_code .= '<object id="player" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="player" width="100%" height="385">
<param name="movie" value="media/jwplayer/player.swf" />
<param name="allowfullscreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="flashvars" value="file=' . $flv_file . '&image=" />
<embed
type="application/x-shockwave-flash"
id="player2"
name="player2"
src="media/jwplayer/player.swf"
width="100%"
height="385"
allowscriptaccess="always"
allowfullscreen="true"
flashvars="file=' . $flv_file . '&image="
/>
</object>';
$element->outertext = $element->outertext . $flv_code;
}
}
if ($flv_found > 0) {
$actu_texte_format = $html->save();
} else {
$actu_texte_format = $texte_a_formater;
}
return $actu_texte_format;
$html->clear();
unset($html);
}
示例7: trim
$html_dom->load($buf);
$toc = '';
$last_level = 0;
foreach ($html_dom->find('h1,h2,h3,h4,h5,h6') as $h) {
$inner_text = trim($h->innertext);
$id = str_replace(' ', '_', $inner_text);
$id = preg_replace('%[^a-zA-Z0-9_-]%', '', $id);
$h->id = $id;
// add id attribute so we can jump to this element
$level = intval($h->tag[1]);
if ($level > $last_level) {
$toc .= "<ul>";
} else {
$toc .= str_repeat('</li></ul>', $last_level - $level);
$toc .= '</li>';
}
$toc .= "<li><a href='#{$id}'>{$inner_text}</a>";
$last_level = $level;
}
$toc .= str_repeat('</li></ul>', $last_level);
// Replace placeholder with this:
$html_dom->find('div[id=word-table-of-contents]', 0)->innertext = $toc;
$html_with_toc = $html_dom->save();
$html_dom->clear();
unset($html_dom);
print $html_with_toc;
?>
</div>
</body>
</html>
示例8: BB_HTMLPurifyForWYMEditor
function BB_HTMLPurifyForWYMEditor($data, $options)
{
if (isset($options["shortcodes"]) && (!$options["shortcodes"] || !isset($options["shortcode_placeholder"]) || !isset($options["shortcode_ids"]))) {
unset($options["shortcodes"]);
}
if (isset($options["validate_img"]) && !$options["validate_img"]) {
unset($options["validate_img"]);
}
// Let HTML Purifier do the heavy-lifting (removes XSS, etc).
// If the 'p' tag ever accepts more than 'class', the 'class' extraction code while generating pretty HTML will need rewriting.
$config = array("Attr.EnableID" => isset($options["shortcodes"]), "HTML.Allowed" => "p[class],strong,em,sup,sub,a[title|href],ul[class],ol[class],li[class],h1[class],h2[class],h3[class],h4[class],h5[class],h6[class],pre[class],blockquote[class],img[" . (isset($options["shortcodes"]) ? "id|class|" : "") . "src|alt]");
if (isset($options["allowed_classes"]) && is_array($options["allowed_classes"])) {
$config["Attr.AllowedClasses"] = $options["allowed_classes"];
}
$data = BB_HTMLPurify($data, $config);
// Replace newlines outside of 'pre' tags with spaces.
$data2 = "";
$lastpos = 0;
$pos = strpos($data, "<pre");
$pos2 = strpos($data, "</pre>");
$pos3 = strpos($data, ">", $pos);
while ($pos !== false && $pos2 !== false && $pos3 !== false && $pos3 < $pos2) {
$data2 .= Str::ReplaceNewlines(" ", substr($data, $lastpos, $pos3 + 1 - $lastpos));
$data2 .= Str::ReplaceNewlines("\n", substr($data, $pos3 + 1, $pos2 - $pos3 - 1));
$data2 .= "</pre>";
$lastpos = $pos2 + 6;
$pos = strpos($data, "<pre", $lastpos);
$pos2 = strpos($data, "</pre>", $lastpos);
$pos3 = strpos($data, ">", $pos);
}
$data = $data2 . Str::ReplaceNewlines(" ", substr($data, $lastpos));
// Process the DOM to create consistent input and output.
require_once ROOT_PATH . "/" . SUPPORT_PATH . "/simple_html_dom.php";
$html = new simple_html_dom();
$html2 = new simple_html_dom();
// Make sure all elements and text are inside a top-level tag.
$html->load("<body>" . $data . "</body>");
$bodytags = array("p" => true, "ul" => true, "ol" => true, "h1" => true, "h2" => true, "h3" => true, "h4" => true, "h5" => true, "h6" => true, "pre" => true, "blockquote" => true);
$rows = $html->find("body text");
foreach ($rows as $row) {
$row2 = $row;
while ($row2->parent()->tag != "body") {
$row2 = $row2->parent();
}
if (!isset($bodytags[$row2->tag])) {
$row2->outertext = "<p>" . $row2->outertext . "</p>";
}
}
$html->load($html->save());
$body = $html->find("body", 0);
$rows = $body->children();
foreach ($rows as $row) {
if (!isset($bodytags[$row->tag])) {
$row->outertext = "<p>" . $row->outertext . "</p>";
}
}
$html->load($html->save());
$rows = $html->find("blockquote text");
foreach ($rows as $row) {
$row2 = $row;
while ($row2->parent()->tag != "blockquote") {
$row2 = $row2->parent();
}
if (!isset($bodytags[$row2->tag])) {
$row2->outertext = "<p>" . $row2->outertext . "</p>";
}
}
$html->load($html->save());
// Clean up 'li' elements. WYMEditor only allows a limited number of tags (a good thing).
$rows = $html->find("li");
foreach ($rows as $row) {
$row->innertext = strip_tags($row->innertext, "<strong><em><sup><sub><a><img><ul><ol><li>");
}
// Replace with spaces.
$data = $html->save();
$data = str_replace(array(" ", " ", "Â "), array(" ", " ", " "), $data);
$html->load($data);
// Process shortcodes or images.
if (isset($options["shortcodes"])) {
// Remove invalid 'img' tags.
$rows = $html->find("img");
foreach ($rows as $row) {
if (!isset($row->class) || $row->class != $options["shortcode_placeholder"] || !isset($row->id) || !isset($options["shortcode_ids"][$row->id])) {
$row->outertext = "";
} else {
$row->src = $options["shortcode_ids"][$row->id];
}
}
$html->load($html->save());
// Move text inside the special 'p.wrap-shortcode' class to separate 'p' tags.
$rows = $html->find("p.wrap-shortcode img");
foreach ($rows as $row) {
$str = $row->parent()->innertext;
$pos = strpos($str, "<img ");
$pos2 = strpos($str, "/>", $pos);
$str2 = substr($str, 0, $pos);
$str3 = substr($str, $pos2 + 2);
$str = substr($str, $pos, $pos2 + 2 - $pos);
if ($str2 != "" || $str3 != "") {
$row->parent()->outertext = ($str2 == "" ? "" : "<p>" . $str2 . "</p>") . "<p class=\"" . $row->parent()->class . "\">" . $str . "</p>" . ($str3 == "" ? "" : "<p>" . $str3 . "</p>");
//.........这里部分代码省略.........
示例9: array
function cc2_bootstrap_comment_form_fields($arrFields = array())
{
$return = $arrFields;
$is_horizontal = get_theme_mod('cc2_comment_form_orientation', 'horizontal') == 'horizontal' ? true : false;
//new __debug( $arrFields, 'arrFields' );
// mostly for reference
$aria_req = ' aria-required="true" ';
$commenter = array('comment_author' => '', 'comment_author_email' => '', 'comment_author_url' => '');
// mostly for reference
$arrDefaultFields = array('author' => '<p class="comment-form-author">' . '<label for="author">' . __('Name', 'domainreference') . '</label> ' . (!empty($req) ? '<span class="required">*</span>' : '') . '<input id="author" name="author" type="text" value="' . esc_attr($commenter['comment_author']) . '" size="30"' . $aria_req . ' /></p>', 'email' => '<p class="comment-form-email"><label for="email">' . __('Email', 'domainreference') . '</label> ' . (!empty($req) ? '<span class="required">*</span>' : '') . '<input id="email" name="email" type="text" value="' . esc_attr($commenter['comment_author_email']) . '" size="30"' . $aria_req . ' /></p>', 'url' => '<p class="comment-form-url"><label for="url">' . __('Website', 'domainreference') . '</label>' . '<input id="url" name="url" type="text" value="' . esc_attr($commenter['comment_author_url']) . '" size="30" /></p>');
if (class_exists('simple_html_dom')) {
// avoid nasty errors if out of some unknown reason the simple_html_dom class failed to be included
$dom = new simple_html_dom();
foreach ($arrDefaultFields as $strFieldName => $strHTML) {
// use the default fields ONLY as reference, NOT for actual parsing!
if (isset($return[$strFieldName]) != false) {
// reset variables
$strEditedHTML = '';
// load snippets
$dom->load($return[$strFieldName]);
// find input tag
$elem = $dom->find('input#' . $strFieldName, 0);
// add class if not already set
$elem->class = cc2_htmlClass::addClass($elem->class, 'form-control');
/**
* Wrap element if horizontal form is enabled
* Also see @http://simplehtmldom.sourceforge.net/manual.htm > How to access the HTML element's attributes? > Tips
*/
if ($is_horizontal != false) {
// find label ...
$label_elem = $dom->find('label', 0);
// .. and add class
$label_elem->class = cc2_htmlClass::addClass($label_elem->class, array('col-md-2', 'col-lg-2', 'control-label'));
// wrap parent element
$elem->parent()->outertext = '<div class="col-md-10 col-lg-10">' . $elem->parent()->outertext . '</div>';
}
// return edited data
$strEditedHTML = $dom->save();
// Optionally group field
if ($is_horizontal != false) {
$strEditedHTML = sprintf('<div class="form-group">%s</div>', $strEditedHTML);
}
if (!empty($strEditedHTML)) {
$return[$strFieldName] = $strEditedHTML;
}
// uncomment the following line for testing purposes (ie. to see whether this was ACTUALLY passed throught the filter or not)
//$return[ $strFieldName ] .= '<!-- parsed field: ' . $strFieldName . ' -->';
}
}
}
return $return;
}
示例10: process_content
//.........这里部分代码省略.........
#article-{$md5} > h1 {
font: bold 18px/18px helvetica neue, helvetica, sans-serif;
margin-top: 0;
}
#article-{$md5} > .meta {
font-size: 9px;
text-transform: none;
}
#article-{$md5} > .meta .short-url {
display: none;
}
#article-{$md5} > .meta .full-url {
display: block;
}
#article-{$md5} > h1,
#article-{$md5} > .meta,
#article-{$md5} p {
padding-left: 0;
padding-right: 0;
}
}
</style>
</head>
<body>
<!-- occupy.here meta start -->
<script id="meta">
var meta = {$json};
</script>
<!-- occupy.here meta end -->
<div id="article-{$md5}">
<!-- occupy.here article start -->
<h1>{$article->title}</h1>
<div class="meta">
{$meta}
</div>
<div class="content">
{$content}
</div>
<!-- occupy.here article end -->
</div>
<ol id="article-{$md5}-links" class="article-links">
</ol>
</body>
</html>
END;
$html = new simple_html_dom();
$html->load($content, true, false);
$images = array();
foreach ($html->find('img') as $img) {
$images[] = urldecode($img->src);
}
$image_data = array();
foreach ($images as $url) {
if (!preg_match('/\\.(\\w+)$/', $url, $matches)) {
continue;
}
list(, $ext) = $matches;
$ext = strtolower($ext);
if (empty($image_types[$ext])) {
continue;
}
$type = $image_types[$ext];
$data = download_file($url);
$tmp = tempnam(GRID_DIR . "/public/uploads/tmp", basename($url));
file_put_contents($tmp, $data);
list($width, $height) = getimagesize($tmp);
unlink($tmp);
$base64 = base64_encode($data);
$data_url = "data:{$type};base64,{$base64}";
if (!empty($data)) {
$img_list = $html->find("img[src={$url}]");
foreach ($img_list as $img) {
$img->src = $data_url;
$img->width = $width;
$img->height = $height;
}
}
}
$links = array();
foreach ($html->find("#article-{$md5} .content a[href]") as $link) {
$links[] = urldecode($link->href);
}
if (!empty($links)) {
$links = '<li>' . implode("</li>\n<li>", $links) . "</li>\n";
list($ol) = $html->find("#article-{$md5}-links");
$ol->innertext = $links;
} else {
list($ol) = $html->find("#article-{$md5}-links");
$ol->outertext = '';
}
return $html->save();
}
}
示例11: _Process_Recieved_Content
public static function _Process_Recieved_Content($_HTML_CONTENT, $_Cung1, $_Cung2, $_Summary, $_SourceUri, $_LinkId, $_ImageLink)
{
if ($_HTML_CONTENT != '') {
// Create a DOM object
require_once Kohana::find_file('classes', 'vendor/simple_html_dom');
$html = new simple_html_dom();
// Load HTML from a string
$html->load($_HTML_CONTENT);
unset($_HTML_CONTENT);
if ($html) {
$story = new Model_Horoscope_XungHop();
$ktra = true;
if ($_Cung1 == '-' || $_Cung2 == '-') {
$ktra = FALSE;
}
$story->cung_1 = $_Cung1;
$story->cung_2 = $_Cung2;
$story->alias = $_Cung1 . '_' . $_Cung2;
if (self::CheckRecordByAlias($story->alias)) {
$story->alias = $_Cung1 . '__' . $_Cung2;
}
$story->tom_tat = $_Summary;
$story->ngay_tao = date("Y-m-d");
$story->url_nguon = $_SourceUri;
$story->auto_get = true;
//begin find elements
#find date post
$date = $html->find('div[class="datetime"]', 0);
if ($date) {
$d = explode(',', $date->plaintext);
if (isset($d[1])) {
//var_dump($d);
//exit;
$d1 = explode(' ', trim($d[1]));
list($ngay, $thang, $nam) = explode('/', $d1[0]);
$story->source_date = date("Y-m-d h:i:s", strtotime($nam . '-' . $thang . '-' . $ngay . ' ' . $d1[1] . ':00'));
} else {
$story->source_date = date("Y-m-d h:i:s");
}
} else {
$story->source_date = date("Y-m-d h:i:s");
}
//find content
$content = $html->find('div[id="content_document"]', 0);
if ($content) {
$string = $content->innertext;
# remove white space
$string = str_replace(array("\r\n", "\r", "\n", "\t"), '', $string);
$string = preg_replace('/(<!--.+?-->)/s', '', $string);
$string = preg_replace('@<a[^>]*>(.*)</a>@ismUx', '$1', $string);
$string = preg_replace('/<p[ ]class="pAuthor">.*<\\/p>/ismxU', '', $string);
$string = preg_replace('/<p[ ]class="pSource">.*<\\/p>/ismxU', '', $string);
$story->noi_dung = $string;
$story->kiem_tra = $ktra;
$story->save();
if ($story->identifier()) {
if ($ktra) {
//get image thumb => save to disk => update record in db
$path = 'assets/horoscope/xung-hop/' . $story->alias . '/';
$img = Vendor_Crawler::get_file_from_url_by_curl($_ImageLink, $save_to_path = $path, $file_name_to_set = $story->alias . '-thumb');
if ($img) {
//check file size, if = 0 -> mean file can't get
if (filesize($img) == 0) {
@copy('assets/horoscope/thumb_140.jpg', $img);
}
$story->hinh_anh = '/' . $img;
} else {
$story->hinh_anh = $_ImageLink;
}
} else {
$story->hinh_anh = $_ImageLink;
}
if ($ktra != FALSE) {
//print_r($img);
$html2 = new simple_html_dom();
$html2->load($story->noi_dung);
$images = $html2->find('img');
if (count($images) > 0) {
for ($i = 0; $i < count($images); $i++) {
unset($images[$i]->onclick);
$file_name = 'anh_' . $i + 1;
$get_file = Vendor_Crawler::get_file_from_url_by_curl($images[$i]->src, $save_to_path = $path, $file_name_to_set = $file_name);
if (filesize(ltrim($get_file, '/')) == 0) {
unset($images[$i]);
} else {
$images[$i]->src = '/' . $get_file;
}
}
}
$story->noi_dung = $html2->save();
$html2->clear();
unset($html2);
} else {
$story->hinh_anh = $_ImageLink;
}
$story->save();
//insert done => update from tmp table
Model_Horoscope_XungHopLinkBLL::UpdateRecordStatus($_LinkId);
self::_print_to_console('Done: ' . $_SourceUri);
} else {
//.........这里部分代码省略.........
示例12: convert
/**
* Convert Embedded CSS to Inline
* @param string $document
* @param bool $strip_class strip attribute class
*/
function convert($document, $strip_class = false)
{
// Debug mode
// Debug mode will output selectors and styles that are detected in the embedded CSS
$debug = false;
// Extract the CSS
preg_match('/<style[^>]+>(?<css>[^<]+)<\\/style>/s', $document, $matches);
// If no CSS style
if (empty($matches)) {
return $document;
}
// Strip out extra newlines and tabs from CSS
$css = preg_replace("/[\n\r\t]+/s", "", $matches['css']);
// Extract each CSS declaration
preg_match_all('/([-a-zA-Z0-9_ ,#\\.]+){([^}]+)}/s', $css, $rules, PREG_SET_ORDER);
// For each CSS declaration, make the selector and style declaration into an array
// Array index 1 is the CSS selector
// Array index 2 is the CSS rule(s)
foreach ($rules as $rule) {
// If the CSS selector is multiple, we should split them up
if (strstr($rule['1'], ',')) {
// Strip out spaces after a comma for consistency
$rule['1'] = str_replace(', ', ',', $rule['1']);
// Unset any previous combos
unset($selectors);
// Make each selector declaration its own
// Create a separate array element in styles array for each declaration
$selectors = explode(',', $rule['1']);
foreach ($selectors as $selector) {
$selector = trim($selector);
if (!isset($styles[$selector])) {
$styles[$selector] = '';
}
$styles[$selector] .= trim($rule['2']);
if ($debug) {
echo $selector . ' { ' . trim($rule['2']) . ' }<br/>';
}
}
} else {
$selector = trim($rule['1']);
if (!isset($styles[$selector])) {
$styles[$selector] = '';
}
$styles[$selector] .= trim($rule['2']);
if ($debug) {
echo $selector . ' { ' . trim($rule['2']) . ' }<br/>';
}
}
}
// DEBUG: Show selector and declaration
if ($debug) {
echo '<pre>';
foreach ($styles as $selector => $styling) {
echo $selector . ':<br>';
echo $styling . '<br/><br/>';
}
echo '</pre><hr/>';
}
// For each style declaration, find the selector in the HTML and add the inline CSS
if (!empty($styles)) {
// Load Simple HTML DOM helper
require_once 'simple_html_dom.php';
$html_dom = new simple_html_dom();
// Load in the HTML without the head and style definitions
$html_dom->load(preg_replace('/\\<head\\>(.+?)\\<\\/head>/s', '', $document));
foreach ($styles as $selector => $styling) {
foreach ($html_dom->find($selector) as $element) {
// Check to make sure the style doesn't already exist
if (!stristr($element->style, $styling)) {
if (strlen($element->style) > 0 && substr(rtrim($element->style), -1) !== ';') {
$element->style .= ';';
}
// If there is any existing style, this will append to it
$element->style .= $styling;
}
}
}
$inline_css_message = $html_dom->save();
// Strip class attribute
if ($strip_class === true) {
$inline_css_message = preg_replace('~(<[a-z0-0][^>]*)(\\s(?:class|id)\\s*=\\s*(([\'"]).*?\\4|[^\\s]*))~usi', '\\1', $inline_css_message);
}
$html_dom->__destruct();
return $inline_css_message;
}
return false;
}
示例13: strip_html
public static function strip_html($html, $scripts = true, $styles = true)
{
$obj = new simple_html_dom();
$obj->load($html);
if ($styles) {
$elems = $obj->find("style");
foreach ($elems as $elem) {
$elem->outertext = "";
}
$elems = $obj->find("link");
foreach ($elems as $elem) {
$elem->outertext = "";
}
}
if ($scripts) {
$elems = $obj->find("script");
foreach ($elems as $elem) {
$elem->outertext = "";
}
}
return $obj->save();
}
示例14: MYPDF
//.........这里部分代码省略.........
$html .= '<h1>' . html_entity_decode($post->post_title, ENT_QUOTES) . '</h1>';
// Display author name is set in config
if (isset($this->options['authorDetail']) and !$this->options['authorDetail'] == '') {
$author = get_the_author_meta($this->options['authorDetail'], $post->post_author);
$html .= '<p><strong>Author : </strong>' . $author . '</p>';
}
// Display category list is set in config
if (isset($this->options['postCategories'])) {
$categories = get_the_category_list(', ', '', $post);
if ($categories) {
$html .= '<p><strong>Categories : </strong>' . $categories . '</p>';
}
}
// Display tag list is set in config
if (isset($this->options['postTags'])) {
$tags = get_the_tags($post->the_tags);
if ($tags) {
$html .= '<p><strong>Tagged as : </strong>';
foreach ($tags as $tag) {
$tag_link = get_tag_link($tag->term_id);
$html .= '<a href="' . $tag_link . '">' . $tag->name . '</a>';
if (next($tags)) {
$html .= ', ';
}
}
$html .= '</p>';
}
}
// Display date if set in config
if (isset($this->options['postDate'])) {
$date = get_the_date($post->the_date);
$html .= '<p><strong>Date : </strong>' . $date . '</p>';
}
// Display featured image if set in config and post/page
if (isset($this->options['featuredImage'])) {
if (has_post_thumbnail($post->ID)) {
$html .= get_the_post_thumbnail($post->ID);
}
}
$html .= htmlspecialchars_decode(htmlentities($post->post_content, ENT_NOQUOTES, 'UTF-8', false), ENT_NOQUOTES);
$dom = new simple_html_dom();
$dom->load($html);
foreach ($dom->find('img') as $e) {
// Try to respect alignment of images
// This code is under heavy development, so well-commented
// First, try to determine the desired alignment from the class attribute inserted by WP.
// Note that as we're still working with HTML vs CSS, and HTML uses "middle" for center, we
// have two variables to fill for that possibility.
if (preg_match('/alignleft/i', $e->class)) {
$imgalign = 'left';
} elseif (preg_match('/alignright/i', $e->class)) {
$imgalign = 'right';
} elseif (preg_match('/aligncenter/i', $e->class)) {
$imgalign = 'center';
$htmlimgalign = 'middle';
} else {
$imgalign = 'none';
}
// These options apply to all images. Remove any embedded class, which is ignored by TCPDF, anyway;
// then set an align attribute inside the img tag (for HTML), and finally, a style tag (for CSS).
$e->class = null;
$e->align = $imgalign;
if (isset($htmlimgalign)) {
$e->style = 'float:' . $htmlimgalign;
} else {
$e->style = 'float:' . $imgalign;
}
// Try to identify SVG images vs JPG or PNG, so that we treat them correctly. Currently, we don't
// handle these well, so we'll just swap them with placeholder links.
// Note that we're still using div tags to (harshly) force images into some semblance of horizontal
// position. This precludes text wrap, and ultimately (if we can get the above working) should be
// replaced (unless we need the text link) with the CSS in the img tag (if TCPDF will respect it).
if (strtolower(substr($e->src, -4)) == '.svg') {
$e->src = null;
$e->outertext = '<div style="text-align:' . $imgalign . '">[ SVG: ' . $e->alt . ' ]</div><br/>';
} else {
$e->outertext = '<div style="text-align:' . $imgalign . '">' . $e->outertext . '</div>';
}
}
$html = $dom->save();
$dom->clear();
// Test TCPDF functions to include here.
// Presently, we're working with trying to get PDF forms working. These options should go into the admin UI.
// set default form properties
$pdf->setFormDefaultProp(array('lineWidth' => 1, 'borderStyle' => 'solid', 'fillColor' => array(255, 255, 200), 'strokeColor' => array(255, 128, 128)));
// Print text using writeHTML
$pdf->writeHTML($html, true, 0, true, 0);
// ---------------------------------------------------------
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
// Create directory if not exist
if (!is_dir(WPPTOPDFENH_CACHE_DIR)) {
mkdir(WPPTOPDFENH_CACHE_DIR, 0777, true);
}
if ($forceDownload) {
$pdf->Output($filePath, 'FI');
} else {
$pdf->Output($filePath, 'F');
}
}
示例15: filter_the_content
public function filter_the_content($content = null)
{
$return = $content;
// sanity check: do not parse content if it doesnt contain html tags, or class-attributes (maybe .. )
if (!empty($return) && strpos($return, '<') !== false && strpos($return, '>') !== false) {
$dom = new simple_html_dom();
$dom->load($return);
/**
* Comment buttons
* @see http://codex.wordpress.org/Function_Reference/comment_form#Default_.24args_array
*
* Original code:
// here for each comment reply link of wordpress
jQuery( '.comment-reply-link' ).addClass( 'btn btn-primary' );
// here for the submit button of the comment reply form
jQuery( '#commentsubmit' ).addClass( 'btn btn-primary' );
*/
foreach ($dom->find('.comment-reply-link, #commentsubmit') as $elem) {
if (!isset($elem->class)) {
$elem->class = 'btn btn-primary';
} elseif (!empty($elem->class) != false && stripos($elem->class, 'btn btn-primary') === false) {
$elem->class = trim($elem->class . ' btn btn-primary');
// better than .=, cause you never know ... might be an empty class="" construct ^_^
}
}
/**
* there can only be ONE id .. ^_^(ID = _unique_ IDentifier)
* NOTE: Redundant / obsolete.
*/
/*
if( stripos($dom->find('#commentsubmit',0)->class, 'btn btn-primary' ) === false ) {
$dom->find('#commentsubmit',0)->class = trim( $dom->find('#commentsubmit', 0)->class . ' btn btn-primary');
}
*/
$return = $dom->save();
}
return $return;
}