在X段php之后插入广告

时间:2017-01-10 20:44:22

标签: php

您好我尝试使用一个类在x段的段落之后插入adsense块。

它使用常规文本,但不适用于adsense代码。

完整代码为HERE

<?php
     namespace keesiemeijer\Insert_Content;

     function insert_content( $content, $insert_content = '', $args = array() ) {

     $args = array_merge( get_defaults(), (array) $args );

     if ( empty( $insert_content ) ) {
    return $content;
}

// Validate arguments
$args['parent_element_id'] = trim( (string) $args['parent_element_id'] );
$args['insert_element']    = trim( (string) $args['insert_element'] );
$args['insert_element']    = $args['insert_element'] ? $args['insert_element'] : 'p';
$args['insert_after_p']    = abs( intval( $args['insert_after_p'] ) );
$parent_element            = false;

// Content wrapped in the parent HTML element (to be inserted).
$insert_content = "<{$args['insert_element']}>{$insert_content}</{$args['insert_element']}>";

$nodes = new \DOMDocument();

// Load the HTML nodes from the content.
@$nodes->loadHTML( $content );

if ( $args['parent_element_id'] ) {
    $parent_element = $nodes->getElementById( $args['parent_element_id'] );

    if ( !$parent_element ) {
        // Parent element not found.
        return $content;
    }

    // Get all paragraphs from the parent element.
    $p = $parent_element->getElementsByTagName( 'p' );
} else {
    // Get all paragraphs from the content.
    $p = $nodes->getElementsByTagName( 'p' );
}

$insert_nodes = new \DOMDocument();
@$insert_nodes->loadHTML( $insert_content );

$insert_element = $insert_nodes->getElementsByTagName( $args['insert_element'] )->item( 0 );

if ( !$insert_element ) {
    return $content;
}

// Get paragraph indexes
$nodelist = get_node_indexes( $p, $args );

// Check if paragraphs are found.
if ( !empty( $nodelist ) ) {

    $insert_index = get_item_index( $nodelist, $args );

    if ( false === $insert_index ) {
        return $content;
    }

    // Insert content after this (paragraph) node.
    $insert_node = $p->item( $insert_index );

    // Insert the nodes
    insert_content_element( $nodes, $insert_node, $insert_element );

    $html = get_HTML( $nodes );
    if ( $html ) {
        $content = $html;
    }
} else {
    // No paragraphs found.
    if ( (bool) $args['insert_if_no_p'] ) {

        if ( $parent_element ) {
            // Insert content after parent element.
            insert_content_element( $nodes, $parent_element, $insert_element );

            $html = get_HTML( $nodes );
            if ( $html ) {
                $content = $html;
            }
        } else {
            // Add insert content after the content/
            $content .= $insert_content;
        }
    }
}

return $content;
 }

/ **  *获取默认参数。  *  * @return数组具有默认参数的数组。  * /

 function get_defaults() {
 return array(
    'parent_element_id' => '',
    'insert_element'    => 'p',
    'insert_after_p'    => '',
    'insert_if_no_p'    => true,
    'top_level_p_only'  => true,
   );
  }


function get_node_indexes( $nodes, $args ) {
$args      = array_merge( get_defaults(), (array) $args );
$nodelist  = array();
$length    = isset( $nodes->length ) ? $nodes->length : 0;
$parent_id = trim( $args['parent_element_id'] );

for ( $i = 0; $i < $length; ++$i ) {
    $nodelist[ $i ] = $i;
    $parent         = false;
    $node           = $nodes->item( $i );

    if ( $parent_id  ) {
        if ( $node->parentNode->hasAttribute( 'id' ) ) {
            $parent_id_attr = $node->parentNode->getAttribute( 'id' );
            $parent = ( $parent_id  === $parent_id_attr );
        }
    } else {
        $parent = ( 'body' === $node->parentNode->nodeName );
    }

    if ( (bool) $args['top_level_p_only'] && !$parent ) {
        // Remove nested paragraphs from the list.
        unset( $nodelist[ $i ] );
    }
}

return array_values( $nodelist );
}


function get_item_index( $nodelist, $args ) {

if ( empty( $nodelist ) ) {
    return false;
}

$args         = array_merge( get_defaults(), (array) $args );
$count        = count( $nodelist );
$insert_index = abs( intval( $args['insert_after_p'] ) );

end( $nodelist );
$last = key( $nodelist );
reset( $nodelist );

if ( !$insert_index ) {
    if ( 1 < $count ) {
        // More than one paragraph found.
        // Get middle position to insert the HTML.
        $insert_index = $nodelist[ floor( $count / 2 ) -1 ];
    } else {
        // One paragraph
        $insert_index = $last;
    }
  } else {
    // start counting at 0.
    --$insert_index;
    --$count;

    if ( $insert_index > $count  ) {
        if ( (bool) $args['insert_if_no_p'] ) {
            // insert after last paragraph.
            $insert_index = $last;
        } else {
            return false;
        }
    }
}

  return $nodelist[ $insert_index ];
  }

 /**
  * Insert an element (and it's child elements) in the content.
  *
  * @param object  $nodes       DOMNodeList instance containing all nodes.
  * @param object  $insert_node DOMElement object to insert nodes after
  * @param object  $insert      DOMElement object to insert
  * @return void
  */
 function insert_content_element( $nodes, $insert_node, $insert_element ) {
$next_sibling = isset( $insert_node->nextSibling ) ? $insert_node->nextSibling : false;

if ( $next_sibling ) {
    // get sibling element (exluding text nodes and whitespace).
    $next_sibling = nextElementSibling( $insert_node );
}

if ( $next_sibling ) {
    // Insert before next sibling.
    $next_sibling->parentNode->insertBefore( $nodes->importNode(   $insert_element, true ), $next_sibling );
} else {
    // Insert as child of parent element.
    $insert_node->parentNode->appendChild( $nodes->importNode(  $insert_element, true ) );
}
}


  function nextElementSibling( $node ) {
while ( $node && ( $node = $node->nextSibling ) ) {
    if ( $node instanceof \DOMElement ) {
        break;
    }
}
return $node;
 }


function get_HTML( $nodes ) {
$body_node = $nodes->getElementsByTagName( 'body' )->item( 0 );

if ( $body_node ) {
    // Convert nodes from the body element to a string containing HTML.
    $content = $nodes->saveHTML( $body_node );
    // Remove first body element only.
    $replace_count = 1;

    return str_replace( array( '<body>', '</body>' ) , array( '', '' ), $content, $replace_count );
}

return '';
}

调用函数

$args = array(
    'insert_after_p' => 3, // Insert after the second paragraph
);

echo keesiemeijer\Insert_Content\insert_content($content, $insert_content, $args ); 

0 个答案:

没有答案