コード例 #1
0
<?php

/* rewrite links in content to open in new windows */
fof_add_render_filter('fof_item_targets');
function fof_item_targets($content)
{
    /* quiet warnings */
    $old_xml_err = libxml_use_internal_errors(true);
    $dom = new DOMDocument();
    /*
    	Load content into DOM, within a div wrapper.  Wrapper div will be
    	stripped before returning altered content.  Without doing this,
    	any bare text content would get wrapped in p elements while being
    	parsed in.
    */
    $dom->loadHtml('<div>' . mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8") . '</div>');
    /* strip <!DOCTYPE> which DOMDocument adds */
    $dom->removeChild($dom->firstChild);
    /* strip <html><body> which DOMDocument adds */
    $dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild);
    /* replace or add link targets */
    $xpath = new DOMXpath($dom);
    foreach ($xpath->query('//a') as $node) {
        $node->setAttribute('target', '_blank');
    }
    $content_out = '';
    /* emit the updated contents inside our div */
    /* start at the first node inside first div.. */
    $node = $dom->firstChild->firstChild;
    while ($node) {
        $content_out .= $dom->saveHTML($node);
コード例 #2
0
<?php

/* load images on-demand */
fof_add_render_filter('fof_images_on_demand');
function fof_images_on_demand($content)
{
    // AJAX refresh: we don't apply this filter, because images are
    // probably already loaded
    if ($_REQUEST['no_img_filter']) {
        return $content;
    }
    /* quiet warnings */
    $old_xml_err = libxml_use_internal_errors(true);
    $dom = new DOMDocument();
    // hack borrowed from http://beerpla.net/projects/smartdomdocument-a-smarter-php-domdocument-class/
    $dom->loadHTML(mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8"));
    foreach ($dom->getElementsByTagName('img') as $img) {
        if ($img->hasAttribute('src')) {
            $img->setAttribute('data-fof-ondemand-src', $img->getAttribute('src'));
            $img->removeAttribute('src');
        }
        if ($img->hasAttribute('srcset')) {
            $img->setAttribute('data-fof-ondemand-srcset', $img->getAttribute('srcset'));
            $img->removeAttribute('srcset');
        }
    }
    $content_out = '';
    $node = $dom->firstChild;
    while ($node) {
        $content_out .= $dom->saveHTML($node);
        /* repeat for all nodes at this level */