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

前端之家收集整理的这篇文章主要介绍了php – 在仅文本段落之间放置广告前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用以下代码在我的内容中放置一些广告代码.
<?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;
}

编辑

从你的评论

>您应该使用以下代码来调试< pre><?PHP var_dump($NAME_OF_VARIABLE); ?>< /预取代.对于您的第一个问题,我将使用?>< pre><?PHP var_dump($paragraph); ?>< / pre><?PHP就在这行$paragraph = explode($closing_p,wpautop($content));准确地看看内容是如何分开的.这将让您了解您的内容是否正确分割.
>还使用?>< pre><?PHP var_dump($p); ?>< / pre><?PHP就在此行之后$p = array();检查特定段落的值.请记住,带有img,li和ul标签的段落应该有0,而且段落少于10个单词.其余的应该是1
>编辑 – >解决了剥离段落的问题.谢谢你指出这个漏洞给我.从未意识到这一点我已经更新了代码,所以只需复制并粘贴它即可

编辑2

请注意,您无法使用所使用的在线工具测试代码.这并不是the_content()输出内容的真实反映.您使用在线工具看到的输出将被过滤并标记,然后作为the_content()发送到屏幕.如果您使用Google Chrome等浏览器检查输出,则会看到正确应用了p标记.

我还在代码中更改了带有image标签的a标签

原文链接:https://www.f2er.com/php/130499.html

猜你在找的PHP相关文章