【问题标题】:Place Ads in between Text-Only Paragraphs在纯文本段落之间放置广告
【发布时间】:2014-11-11 09:00:21
【问题描述】:

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

<?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, 0, $halfway_mark));
$second_half_content = implode(' ', array_slice($content, $halfway_mark));
echo $first_half_content.'...';
echo ' YOUR ADS CODE';
echo $second_half_content;
?>

如何修改它,以便在纯文本段落之间同时放置 2 个广告(包含广告的 &lt;p&gt;...&lt;/p&gt; 都不应包含图像或嵌入视频)。

我想避免使用 jQuery。

我的用例示例...

  • 我想在本文中插入 2 个广告块。
  • 我希望第一个广告块位于第一段之后。但第 1 段中的任何图片都应删除。
  • 对于第二个广告,它应该放置在文章后半部分的纯文本段落之间,以便广告代码夹在大量文本之间,并且永远不会非常靠近嵌入的图像或视频等。
  • 2 个广告之间应至少有 2 个段落

【问题讨论】:

  • “等距放置 3 个广告”,即水平放置 3 个 div?
  • @David - 垂直方向,如 1para 2para AD 3para 4 para ....
  • 你不打算用这么多广告扼杀你的网站吗?如果它们是 google adsense 广告,请小心。
  • 如果没有没有图像或视频的段落怎么办。不可能没有。的用例。你真正想要实现什么
  • 在内容丰富的网站中...这是不可能的。我们有一个用例,如果不是第三个,第二个应该放在最后。

标签: php wordpress


【解决方案1】:
$content = apply_filters('the_content', $post->post_content);
// $middle will mark the center of the $content based on number of characters and is aware of words and will therefor mark the middle to the nearest space. +1 is added to then remove the whitespace at the end of the string.
$middle = strrpos(substr($content, 0, floor(strlen($content) / 2)), ' ') + 1;
// $first_half will substring the text to the first half based on where $middle says to split it
$first_half= substr($text, 0, $middle);
// $second_half substrings the text to the seconf half based on where $first_half is split.
$second_half= substr($text, $middle);
echo $first_half.'...';
echo ' YOUR ADS CODE';
echo $second_half;

【讨论】:

  • 我认为,当您为您的意图添加一些解释时,这对 OP 和更多访问者会更有帮助。
  • 您在哪里检查嵌入的图像或视频?
【解决方案2】:

这是一种解决方案。它不是完全程序化的,但我以前做过,它会起作用。基本上,使用“简码”。完全以编程方式执行此操作的问题在于,没有很好的方法来确定带有内嵌图像、视频等的文章的流程是否会导致广告展示位置看起来非常糟糕。相反,请使用简码设置您的 CMS,以便编辑者可以将广告放置在文章中的最佳位置。

例如

<p>Bacon ipsum dolor sit amet short ribs tenderloin venison pastrami meatloaf kevin, shoulder meatball landjaeger pork corned beef turkey salami frankfurter jerky. Pork loin bresaola porchetta strip steak meatball t-bone andouille chuck chicken shankle shank tongue. Hamburger flank kevin short ribs. Pork loin landjaeger frankfurter corned beef, fatback salami short loin ground round biltong.</p>

[ad_block]

<p>Pastrami jerky drumstick swine ribeye strip steak pork belly kevin tail rump pancetta capicola. Meatloaf doner porchetta, rump tenderloin t-bone biltong pork belly. Porchetta boudin ham ribeye frankfurter flank short loin, drumstick pork loin filet mignon chuck fatback. Strip steak jowl capicola ham hock turducken biltong ground round filet mignon venison prosciutto chuck pork. Venison ribeye fatback kielbasa, ball tip turducken capicola drumstick sausage pancetta boudin turkey ham strip steak corned beef.</p>

然后,使用 PHP 的 str_replace,您可以简单地将 ad_block 短代码替换为您的广告的 HTML。

例如

echo str_replace('[ad_block]', $ad_block_html, $content);

【讨论】:

  • 完全同意...但这仅对相当新的网站有意义。一个已经很大的网站的噩梦,单一的管理。
  • 感谢您的培根...抱歉,我没有解决整个问题! :)
【解决方案3】:

好的,这是一个让您(可能)朝着更接近您所要求的完全编程方式的方向发展的想法......

使用像 http://simplehtmldom.sourceforge.net/ 这样的 HTML DOM 解析器来解析每篇文章。使用这个,理论上你应该能够挑选出所有的段落标签,然后根据你的数学在正确的标签之间插入广告块。

$html = str_get_html($content);
$paragraphs_arr = $html->find('p'); //Returns all paragraphs
$halfway_mark = ceil(count($paragraphs_arr) / 2);

$halfway_paragraph = $paragraphs_arr[$halfway_mark];

// Somewhere here now you just have to figure out how to inject the ad right after the halfway paragraph.

E.g.
$p_content = $halfway_paragraph->innertext();
$p_content = $p_content."</p><p><!-- Ad --></p>"; // Force close the tag as we're in the innertext.
$halfway_paragraph->innertext($p_content);

这是否更接近您想要做的事情?

【讨论】:

    【解决方案4】:

    使用你给我的示例代码,这是我能做的最好的,那里有太多的图像,你需要一个天才来找出你的要求所需的逻辑,但试试它可能不会太远了。为此,您需要 php 5.5。

    需要注意的几点:
    1. 它将段落标识为包裹在 p 元素中,而不是可视段落。
    2. 如果 p 元素存在于其他元素中,它也会将它们识别为段落。第一个广告就是一个例子。避免在块引用、列表等中使用 p,这不是必需的,而是使用 spans、div 来代替文本。
    3. 我在 __construct 中注释了调用函数的行,取消注释以插入第二张图像。这实际上效果很好,但是您的内容有很多 p 元素,句子分成多个 p,这不太可能成为实际内容的一个因素。
    4. 搜索para 1 + para 2中的图片并删除它们。

    $content = apply_filters('the_content', get_the_content() ); 
    
        class adinsert {
    
    
        var $content;
        var $paragraphs;    
        var $ad_pos1=2;
        var $ad_pos2;
        var $ad_pos3;
        var $ad= '<h1>ad position here</h1>';
    
    public function __construct($content) {
    
        if(!$content)
            return $content;
    
        $this->set_content($content);
    
        $this->paragrapherize(2);
        $this->paragraph_numbers();
        $this->get_first_pos();
        $this->paragrapherize();
        $this->paragraph_numbers();
        $this->find_images();
        $this->find_ad_pos(); 
        $this->insert_ads();
    
    }
    
    
    public function echo_content(){
        echo $this->content;
    }
    
    private function insert_ads() {
    
        if($this->ad_pos2 && $this->ad_pos2 != 'end'):
            $posb= $this->ad_pos2;
            $this->content=substr_replace($this->content,$this->ad,$posb ,0);
        else:
            $this->content.= $this->ad;    
        endif;
    
        //comment the below line to remove last image insertion 
        $this->content.= $this->ad;
    }
    
    private function get_first_pos() {
    
        $i=0;
    
        foreach($this->paragraphs as $key=>$data):
            if($i==0):
    
                $length= $data['end']-$data['start'];
                $string= substr($this->content, $data['start'],$length);    
                $newkey= $key+1;
                $lengthb= $this->paragraphs[$newkey]['end']-$this->paragraphs[$newkey]['start'];
                $stringb= substr($this->content, $this->paragraphs[$newkey]['start'],$lengthb);
    
                $wcount= count(explode(' ', $string));
    
                if( preg_match('/(<img[^>]+>)/i', $string, $image) ):
                        $newstring=preg_replace('/(<img[^>]+>)/i', '', $string);
    
                            if($wcount>10):
                                $newstring.=$this->ad;
                                $this->ad_pos1=1;       
                                $this->content=str_replace($string,$newstring,$this->content);
                            endif;
                else:
                            if($wcount>10) :
                                $newstring=$string.$this->ad;
                                echo $newstring;
                                $this->ad_pos1=1;
                                //$this->content=str_replace($string,$newstring,$this->content);
                                $this->content= preg_replace('~'.$string.'~', $newstring, $this->content, 1);
                            endif;
                endif;
    
                if( preg_match('/(<img[^>]+>)/i', $stringb, $imageb) ):
                            $newstringb=preg_replace('/(<img[^>]+>)/i', '', $stringb);  
                            if($wcount<10) :
                            $newstringb.=$this->ad;
                            $this->ad_pos1=2;
                            $this->content=str_replace($stringb,$newstringb,$this->content);
                            endif;
                else:
                            if($wcount<10) :
                                $newstring=$stringb.$this->ad;
                                $this->ad_pos1=2;
                                $this->content=str_replace($stringb,$newstringb,$this->content);
                            endif;
                endif;
    
            else:
                break;
            endif;
            $i++;       
        endforeach;
    }
    
    
    private function find_ad_pos() {
    
        $remainder_images= $this->paragraph_count;
        if($remainder_images < $this->ad_pos1 + 3):
            $this->ad_pos2='end';
        else:   
    
            foreach($this->paragraphs as $key=>$data):
                $p[]=$key;
            endforeach;
    
            unset($p[0]);
            unset($p[1]);
    
            $startpos= $this->ad_pos1 + 2;
            $possible_ad_positions= $remainder_images - $startpos;
        //figure out half way
            if($remainder_images < 3): //use end pos
                $pos1= $startpos;
                $pos1=$this->getclosestkey($pos1, $p);
            else: // dont use end pos
                $pos1=  ($remainder_images/2)-1;
                $pos1= $this->getclosestkey($pos1, $p);
            endif;
            $this->ad_pos2= $this->paragraphs[$pos1]['end'];
        endif;
    }
    
    
    private function getclosestkey($key, $keys) {
        $close= 0;
        foreach($keys as $item): //4>4
            if($close == null || $key - $close > $item - $key ) :
              $close = $item;
            endif;
        endforeach;
        return $close;
    }
    
    
    
    private function find_images() {
    
        foreach($this->paragraphs as $item=>$key):
            $length= $key['end']-$key['start'];
            $string= substr($this->content, $key['start'],$length);
            if(strpos($string,'src')!==false && $item !=0 && $item !=1):
                //unset the number, find start in paragraphs array + 1 after
                unset($this->paragraphs[$item]);
                $nextitem= $item+1;
                $previtem= $item-1;
                unset($this->paragraphs[$nextitem]);
                unset($this->paragraphs[$previtem]);
            endif;          
        endforeach;
    
    }
    
    
    
    
    
    private function paragraph_numbers() {
    
        $i=1;
        foreach($this->paragraphs as $item):
            $i++;
        endforeach; 
        $this->paragraph_count=$i;
    }
    
    private function paragrapherize($limit=0) {
    
        $current_pos=0;
        $i=0;
    
        while( strpos($this->content, '<p', $current_pos) !== false ):
    
        if($limit && $i==$limit)
            break;
    
        if($i==105) {
            break;
        }
            if($i!=0) {
                $current_pos++; 
            }
    
    
            $paragraph[$i]['start']=strpos($this->content, '<p', $current_pos);//1
    
        //looking for the next time a /p follows a /p so is less than the next position of p
    
        $nextp= strpos($this->content, '<p', $paragraph[$i]['start']+1); //14 failing on next???
        $nextendp= strpos($this->content, '</p>', $current_pos);//22
    
        if($nextp>$nextendp)://NO
            $paragraph[$i]['end']=$nextendp;
            if( ($nextendp - $paragraph[$i]['start']) < 80 ):
                unset($paragraph[$i]);
            endif;
    
            $current_pos= $nextendp;
            $i++;   
        else:   
    
        $startipos = $nextendp;
    
            $b=0;                                           
            do {
                if($b==100){
                   break;
                }
    
                $nextp= strpos($this->content, '<p', $startipos); //230
                $nextendp= strpos($this->content, '</p>', $startipos+1);//224
    
    
                if($nextp>$nextendp) {
    
                    $paragraph[$i]['end']=$nextendp;
                    $current_pos= $nextendp;
    
                    $i++;
                } else {
                    $startipos = $nextendp+1;
                }
                $b++;
    
            } while ($nextp < $nextendp );
        endif;
            endwhile;
            $this->paragraphs= $paragraph;
        }
    
        public function set_content($content) {
            $this->content= $content;
        }
    
    }
    
    $newcontent= new adinsert($content);
    

    然后你想输出你的内容

     <?php echo $newcontent->echo_content(); ?>
    

    【讨论】:

    • 是的,它起作用了... :) 1. 但是第三个广告被放置在图像之前eval.in/private/2f5c211b9788c4 2. 它还删除了现有的 div 块。 3. 2 个广告之间应至少有 2 个段落
    • 它还删除了现有的非 p 块,例如 div table
    • 您可以将实际的 html 内容粘贴到某处,以便我了解典型输出吗?它被设计为使用 p 作为外包装,因为这是我假设每个块包含的内容。还可以查看使用$ad_avail_positions 的 if else 块,它决定何时放置广告……通常会有多少段?如果它的方差范围很广,则该功能往往会有点臃肿,除非您可以使用简单的东西,例如如果在此块中使用广告,如果没有图像,则将下一个广告放在 2 块之外,或者如果图像,则跳到下一个可用,等等!
    • 您可以在此处查看我为$content 定义的内容eval.in/private/2f5c211b9788c4 注意:表格、列表不在html 中的p 块内部形成......它们在外部。我认为我们可以在 getElementsByTagName 中包含这些常见标记。你怎么看
    • 之外的常用标签有

    【解决方案5】:

    我做了一些返工,这就是我最终得到的。 随意测试并给我一些反馈。

    class Advertiser
    {
        /**
         * All advertises to place
         *
         * @access  private
         * @var     array
         */
        private $ads        = array();
    
        /**
         * The position behind the </p> element nearest to the center
         *
         * @access  private
         * @var     int
         */
        private $center     = null;
    
        /**
         * The content to parse
         *
         * @access  private
         * @var     string
         */
        private $content    = null;
    
        /**
         * Constructor method
         *
         * @access  public
         * @param   string  $content    the content to parse (optional)
         * @param   array   $ads        all advertises to place (optional)
         * @return  object              itself as object
         */
        public function __construct ($content = null, $ads = array())
        {
            if (count($ads)) $this->ads = $ads;
            if ($content) $this->setContent($content);
    
            return $this;
        }
    
        /**
         * Calculates and sets the position behind the </p> element nearest to the center
         *
         * @access  public
         * @return  object              the position behind the </p> element nearest to the center
         */
        public function calcCenter ()
        {
            $content = $this->content;
    
            if (!$content) return $this;
    
            $center = ceil(strlen($content)/2);
    
            $rlpos  = strripos(substr($content, 0, $center), '</p>');
            $rrpos  = stripos($content, '</p>', $center);
    
            $this->center = 4 + ($center-$rlpos <= $rrpos-$center ? $rlpos : $rrpos);
    
            return $this;
        }
    
        /**
         * Places the first ad
         *
         * @access  public
         * @param   string  $ad optional; if not specified, take the internally setted ad
         * @return  object      itself as object
         */
        public function placeFirstAd ($ad = null)
        {
            $ad = $ad ? $ad : $this->ads[0];
            $content = $this->content;
    
            if (!$content || !$ad) return $this;
    
            // the position before and after the first paragraph
            $pos1 = strpos($content, '<p');
            $pos2 = strpos($content, '</p>') + 4;
    
            // place ad
            $content = substr($content, 0, $pos2) . $ad . substr($content, $pos2);
    
            // strip images
            $content = substr($content, 0, $pos1) . preg_replace('#<img(?:\s.*?)?/?>#i', '', substr($content, $pos1, $pos2)) . substr($content, $pos2);
    
            $this->content = $content;
    
            return $this;
        }
    
        /**
         * Places the second ad
         *
         * @access  public
         * @param   string  $ad optional; if not specified, take the internally set ad
         * @return  object      itself as object
         */
        public function placeSecondAd ($ad = null)
        {
            $ad = $ad ? $ad : $this->ads[1];
            $content = $this->content;
    
            if (!$content || !$ad) return $this;
    
            $center = $this->center;
    
            // place ad
            $content = substr($content, 0, $center) . $ad . substr($content, $center);
    
            $this->content = $content;
    
            return $this;
        }
    
        /* Getters */
    
        /**
         * Gets the content in it's current state
         *
         * @access  public
         * @return  string  the content in it's current state
         */
        public function getContent ()
        {
            return $this->content;
        }
    
        /* Setters */
    
        /**
         * Sets the content
         *
         * @access  public
         * @param   string  $content    the content to parse
         * @return  object              itself as object
         */
        public function setContent ($content)
        {
            $this->content = $content;
    
            $this->calcCenter();
    
            return $this;
        }
    
        /**
         * Sets the first ad
         *
         * @access  public
         * @param   string  $ad the ad
         * @return  object      itself as object
         */
        public function setFirstAd ($ad)
        {
            if ($ad) $this->ad[0] = $ad;
    
            return $this;
        }
    
        /**
         * Sets the second ad
         *
         * @access  public
         * @param   string  $ad the ad
         * @return  object      itself as object
         */
        public function setSecondAd ($ad)
        {
            if ($ad) $this->ad[1] = $ad;
    
            return $this;
        }
    }
    

    使用示例:

    $first_ad   = 'bacon';
    $second_ad  = 'ham';
    
    $content    = apply_filters('the_content', $post->post_content);
    
    $advertiser = new Advertiser($content);
    
    $advertiser->placeFirstAd($first_ad);
    //$advertiser-> placeSecondAd($second_ad);
    
    $advertised_content = $advertiser->getContent();
    

    您可以将 placeSecondAd() 注释掉或用您的工作函数替换它。

    【讨论】:

    • 你在哪里添加第一个广告
    • 警告:preg_match_all():未知修饰符“p”在
    • 第二个广告应放置在没有任何图像的段落之间。看图片。谢谢
    • @DebajyotiDas 抱歉,我忘了添加第一个广告... :D 我还更正了表达式中的错字,所以它现在应该可以工作了。因为我没有安装 PHP,所以我对所有东西都进行了编程,所以很遗憾,我无法对其进行测试。如果有任何其他错误,请通知我。
    • 谢谢... 注意:第二个广告应放置在没有任何图像的段落之间。查看相关图片。
    【解决方案6】:

    使用

     strip_tags('contnet','like <p>');
    

    【讨论】:

    • 在此回答问题如何
    【解决方案7】:

    这是我对这个问题的看法。很抱歉发布的有点晚(并且错过了赏金:-(),但这是忙碌的一周,所以我做了所有的点点滴滴。

    快速启动

    • 我没有删除第一段中的附件。我真的不明白为什么必须为了广告而破坏内容

    • 我在我的代码中做了很多检查,以确保广告只插入纯文本段落之间。纯文本段落被视为不包含 img、li 和 ul 标记的段落

    • 我已经正确记录了每个代码块,因此您可以轻松完成每个部分。我还添加了todo doc 块,如果需要,您需要注意这些块

    • 我已经使用wptexturize 将p 标签应用于the_content。每个双换行符构成一个段落

    • 修改并使用您认为合适的代码。我已经测试过了,所以它在我这边没有错误。

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

    <?php
    add_filter( 'the_content', 'so_25888630_ad_between_paragraphs' );
    
    function so_25888630_ad_between_paragraphs($content){
        /**-----------------------------------------------------------------------------
         *
         *  @author       Pieter Goosen <http://stackoverflow.com/users/1908141/pieter-goosen>
         *  @return       Ads in between $content
         *  @link         http://stackoverflow.com/q/25888630/1908141
         * 
         *  Special thanks to the following answers on my questions that helped me to
         *  to achieve this
         *     - http://stackoverflow.com/a/26032282/1908141
         *     - http://stackoverflow.com/a/25988355/1908141
         *     - http://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, 0, $midpoint );
                if( $count%2 == 1 ) {
                    $second = array_slice( $paragraphs, $midpoint, $midpoint, true );
                }else{
                    $second = array_slice( $paragraphs, $midpoint, $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;
    }
    

    编辑

    来自您的 cmets:

    • 您应该使用以下代码来调试&lt;pre&gt;&lt;?php var_dump($NAME_OF_VARIABLE); ?&gt;&lt;/pre&gt;。对于您的第一个问题,我将在 $paragraphs = explode( $closing_p, wpautop($content) ); 这一行之后使用 ?&gt;&lt;pre&gt;&lt;?php var_dump($paragraphs); ?&gt;&lt;/pre&gt;&lt;?php 来准确查看内容是如何拆分的。这将使您了解您的内容是否被正确拆分。

    • 还可以在此行之后使用?&gt;&lt;pre&gt;&lt;?php var_dump($p); ?&gt;&lt;/pre&gt;&lt;?php $p = array(); 来检查赋予特定段落的值。请记住,带有 img、li 和 ul 标签的段落应该有一个0,少于 10 个单词的段落也是如此。其余的应该有一个 1

    • 编辑 -> 已解决剥离段落的问题。感谢您向我指出这个缺陷。从来没有意识到这一点。我已经更新了代码,所以只需复制粘贴即可

    编辑 2

    请注意,您无法使用您正在使用的在线工具测试您的代码。这并不是the_content() 输出内容的真实反映。您使用在线工具看到的输出在发送到屏幕之前被过滤和标记为the_content()。如果您使用 google chrome 之类的浏览器检查您的输出,您将看到正确应用了 p 标签。

    我还把代码中的a标签改成了image标签

    【讨论】:

    • 天啊,这就像一个魅力。非常感谢,因为我将用这个更新我的网站。非常努力地在很多方面都失败了,但失败了。可靠的代码。所有要求都通过了。 :) 尊敬的……你应该得到赏金。我会记住这一点,并在我有更多自己的时候奖励你一个。
    • 注意:使用&lt;/p&gt; 爆炸会创建没有&lt;/p&gt; 的输出。所以我正在使用$paragraphs = preg_split("/(?=&lt;\/p&gt;)/", $content, null, PREG_SPLIT_DELIM_CAPTURE);,这似乎工作正常。
    【解决方案8】:

    这是一个聪明的方法:

    $content = "<p>A</p><p>B</p><p>C</p><p>D</p>";
    $pos = 2;
    $content = preg_replace('/<p>/', '<helper>', $content, $pos + 1); //<helper>A</p><helper>B</p><helper>C</p><p>D</p>
    $content = preg_replace('/<helper>/', '<p>', $content, $pos);     //<p>A</p><p>B</p><helper>C</p><p>D</p>
    $content = str_replace("<helper>", "<p>ad</p><p>", $content);     //<p>A</p><p>B</p><p>ad</p><p>C</p><p>D</p>
    

    这是一个完整的函数:

    function insertAd($content, $ad, $pos = 0){
      // $pos = 0 means randomly position in the content
      $count = substr_count($content, "<p>");
      if($count == 0  or $count <= $pos){
        return $content;
      }
      else{
        if($pos == 0){
          $pos = rand (1, $count - 1);
        }
        $content = preg_replace('/<p>/', '<helper>', $content, $pos + 1);
        $content = preg_replace('/<helper>/', '<p>', $content, $pos);
        $content = str_replace('<helper>', $ad . "\n<p>", $content);
        return $content;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多