加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

php – 在仅文本段落之间放置广告

发布时间:2020-05-22 15:31:26 所属栏目:PHP 来源:互联网
导读:我使用以下代码在我的内容中放置一些广告代码. ?php$content = apply_filters(the_content, $post-post_content);$content = explode ( , $content);$halfway_mark = ceil(count($content) / 2);$first_half_content = implode(

我使用以下代码在我的内容中放置一些广告代码.

<?php
$content = apply_filters('the_content',$post->post_content);
$content = explode (' ',$content);
$halfway_mark = ceil(count($content) / 2);
$first_half_content = implode(' ',array_slice($content,$halfway_mark));
$second_half_content = implode(' ',$halfway_mark));
echo $first_half_content.'...';
echo ' YOUR ADS CODE';
echo $second_half_content;
?>

如何修改此设置,以便我可以在仅文本段落之间同时放置2个广告(包含广告的< p> …< / p>不应包含图片或嵌入的视频).

我想避免使用jQuery.

我的用例示例……

>我想在本文中插入2个广告块.
>我希望第一个广告块在第一段之后.但是应删除第1段中的任何图像.
>对于第二个广告,它应该放在文章的后半部分中,在纯文本段落之间,这样广告代码在大量文本之间切换,并且永远不会非常接近嵌入的图像或视频等.
> 2个广告之间至少应有2个段落

这是我对这个问题的处理方法.很抱歉发布有点迟了(并且错过了赏金:-(),但这是一个忙碌的一周,所以我做了一切都是点点滴滴.

快速响彻

>我没有删除第一段中的附件.我真的不明白为什么内容必须为广告而被破坏
>我在代码中做了很多检查,试图确保广告仅插入文本段落之间.仅文本段落被视为不包含img,li和ul标记的段落
>我已正确记录每个代码块,因此您可以轻松完成每个部分.如果有必要,我还添加了你需要注意的todo doc块
>我使用wptexturize将p标签应用于the_content.每个双折线构成一个段落
>根据需要修改并使用此代码.我已经对它进行了测试,因此我身上没有错误.

这是代码.希望这对你有用. * PS!所有这些都进入你的functions.php.您的模板文件不需要任何其他代码或mod

<?php
add_filter( 'the_content','so_25888630_ad_between_paragraphs' );

function so_25888630_ad_between_paragraphs($content){
    /**-----------------------------------------------------------------------------
     *
     *  @author       Pieter Goosen <https://stackoverflow.com/users/1908141/pieter-goosen>
     *  @return       Ads in between $content
     *  @link         https://stackoverflow.com/q/25888630/1908141
     * 
     *  Special thanks to the following answers on my questions that helped me to
     *  to achieve this
     *     - https://stackoverflow.com/a/26032282/1908141
     *     - https://stackoverflow.com/a/25988355/1908141
     *     - https://stackoverflow.com/a/26010955/1908141
     *     - http://wordpress.stackexchange.com/a/162787/31545
     *
    *------------------------------------------------------------------------------*/ 
    if( in_the_loop() ){ //Simply make sure that these changes effect the main query only

        /**-----------------------------------------------------------------------------
         *
         *  wptexturize is applied to the $content. This inserts p tags that will help to  
         *  split the text into paragraphs. The text is split into paragraphs after each
         *  closing p tag. Remember,each double break constitutes a paragraph.
         *  
         *  @todo If you really need to delete the attachments in paragraph one,you want
         *        to do it here before you start your foreach loop
         *
        *------------------------------------------------------------------------------*/ 
        $closing_p = '</p>';
        $paragraphs = explode( $closing_p,wptexturize($content) );

        /**-----------------------------------------------------------------------------
         *
         *  The amount of paragraphs is counted to determine add frequency. If there are
         *  less than four paragraphs,only one ad will be placed. If the paragraph count
         *  is more than 4,the text is split into two sections,$first and $second according
         *  to the midpoint of the text. $totals will either contain the full text (if 
         *  paragraph count is less than 4) or an array of the two separate sections of
         *  text
         *
         *  @todo Set paragraph count to suite your needs
         *
        *------------------------------------------------------------------------------*/ 
        $count = count( $paragraphs );
        if( 4 >= $count ) {
            $totals = array( $paragraphs ); 
        }else{
            $midpoint = floor($count / 2);
            $first = array_slice($paragraphs,$midpoint );
            if( $count%2 == 1 ) {
                $second = array_slice( $paragraphs,$midpoint,true );
            }else{
                $second = array_slice( $paragraphs,$midpoint-1,true );
            }
            $totals = array( $first,$second );
        }

        $new_paras = array();   
        foreach ( $totals as $key_total=>$total ) {
            /**-----------------------------------------------------------------------------
             *
             *  This is where all the important stuff happens
             *  The first thing that is done is a work count on every paragraph
             *  Each paragraph is is also checked if the following tags,a,li and ul exists
             *  If any of the above tags are found or the text count is less than 10,0 is 
             *  returned for this paragraph. ($p will hold these values for later checking)
             *  If none of the above conditions are true,1 will be returned. 1 will represent
             *  paragraphs that qualify for add insertion,and these will determine where an ad 
             *  will go
             *  returned for this paragraph. ($p will hold these values for later checking)
             *
             *  @todo You can delete or add rules here to your liking
             *
            *------------------------------------------------------------------------------*/ 
            $p = array();
            foreach ( $total as $key_paras=>$paragraph ) {
                $word_count = count(explode(' ',$paragraph));
                if( preg_match( '~<(?:img|ul|li)[ >]~',$paragraph ) || $word_count < 10 ) {  
                    $p[$key_paras] = 0; 
                }else{
                    $p[$key_paras] = 1; 
                }   
            }

            /**-----------------------------------------------------------------------------
             *
             *  Return a position where an add will be inserted
             *  This code checks if there are two adjacent 1's,and then return the second key
             *  The ad will be inserted between these keys
             *  If there are no two adjacent 1's,"no_ad" is returned into array $m
             *  This means that no ad will be inserted in that section
             *
            *------------------------------------------------------------------------------*/ 
            $m = array();
            foreach ( $p as $key=>$value ) {
                if( 1 === $value && array_key_exists( $key-1,$p ) && $p[$key] === $p[$key-1] && !$m){
                    $m[] = $key;
                }elseif( !array_key_exists( $key+1,$p ) && !$m ) {
                    $m[] = 'no-ad';
                }
            } 

            /**-----------------------------------------------------------------------------
             *
             *  Use two different ads,one for each section
             *  Only ad1 is displayed if there is less than 4 paragraphs
             *
             *  @todo Replace "PLACE YOUR ADD NO 1 HERE" with your add or code. Leave p tags
             *  @todo I will try to insert widgets here to make it dynamic
             *
            *------------------------------------------------------------------------------*/ 
            if( $key_total == 0 ){
                $ad = array( 'ad1' => '<p>PLACE YOUR ADD NO 1 HERE</p>' );
            }else{
                $ad = array( 'ad2' => '<p>PLACE YOUR ADD NO 2 HERE</p>' );
            }

            /**-----------------------------------------------------------------------------
             *
             *  This code loops through all the paragraphs and checks each key against $mail
             *  and $key_para
             *  Each paragraph is returned to an array called $new_paras. $new_paras will
             *  hold the new content that will be passed to $content.
             *  If a key matches the value of $m (which holds the array key of the position
             *  where an ad should be inserted) an add is inserted. If $m holds a value of
             *  'no_ad',no ad will be inserted
             *
            *------------------------------------------------------------------------------*/ 
            foreach ( $total as $key_para=>$para ) {
                if( !in_array( 'no_ad',$m ) && $key_para === $m[0] ){
                    $new_paras[key($ad)] = $ad[key($ad)];
                    $new_paras[$key_para] = $para;
                }else{
                    $new_paras[$key_para] = $para;
                }
            }
        }

        /**-----------------------------------------------------------------------------
         *
         *  $content should be a string,not an array. $new_paras is an array,which will
         *  not work. $new_paras are converted to a string with implode,and then passed
         *  to $content which will be our new content
         *
        *------------------------------------------------------------------------------*/ 
        $content =  implode( ' ',$new_paras );
    }
    return $content;
}

编辑

从你的评论:

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读