【问题标题】:PHP insert ad after X paragraphs, then every Y paragraphs if the text is long enoughPHP 在 X 段之后插入广告,如果文本足够长,则每隔 Y 段插入广告
【发布时间】:2018-09-28 20:43:24
【问题描述】:

我需要这样间隔插入一个广告代码:

  1. $insert_every_paragraphs 是一个变量,指示段落在文本中出现的频率。例如 10 个。
  2. $minimum_paragraph_count 用于查看文本是否足够长以容纳任何广告,另外第一个广告应该出现在这么多段落之后(然后它应该重复 #1 中的间隔)。 4,例如。
  3. 广告代码的工作方式如下:x100、x101、x102、x103 等。用户可以设置有多少可以添加。它们像 [cms_ad:x100] 一样呈现,PHP 稍后替换它们(这部分工作正常)。
  4. 代码在 #3 结束时应该停止添加广告。也就是说,即使它有足够的段落来继续添加广告,它也应该在达到 x104 时停止(在本例中)。

所以在这个例子中,它会这样工作:在 4 个段落之后,插入 x100。然后,每 10 个段落插入另一个,直到插入 x103。不要插入 x104,即使有 100 个段落。而且,如果没有至少 34 个段落,x103 将永远不会出现。

到目前为止,我所拥有的是:

$paragraph_end = '</p>';

$insert_every_paragraphs = 10;
$minimum_paragraph_count = 4;

$embed_prefix      = 'x';
$start_embed_id    = 'x100';
$start_embed_count = intval( str_replace( $embed_prefix, '', $start_embed_id ) ); // ex 100
$end_embed_id      = 'x103';
$end_embed_count   = intval( str_replace( $embed_prefix, '', $end_embed_id ) ); // ex 104

$paragraph_positions = array();
$last_position       = -1;

while ( stripos( $content, $paragraph_end, $last_position + 1 ) !== false ) {
    // Get the position of the end of the next $paragraph_end.
    $last_position         = stripos( $content, $paragraph_end, $last_position + 1 ) + 3;
    $paragraph_positions[] = $last_position;
}

// If the total number of paragraphs is bigger than the minimum number of paragraphs
if ( count( $paragraph_positions ) >= $minimum_paragraph_count ) {
    // How many ads have been added?
    $n = $start_embed_count;
    // Store the position of the last insertion.
    $previous_position = $start_embed_count;
    $i                 = 0;
    while ( $i < count( $paragraph_positions ) && $n <= $end_embed_count ) {

        if ( 0 === ( $i + 1 ) % $insert_every_paragraphs && isset( $paragraph_positions[ $i ] ) ) {
            $shortcode = "\n" . '[cms_ad:' . $embed_prefix . (int) $n . ']' . "\n";

            $position = $paragraph_positions[ $i ] + 1;

            if ( $position > $previous_position ) {
                $content = substr_replace( $content, $shortcode, $paragraph_positions[ $i ] + 1, 0 );
                // Increase the saved last position.
                $previous_position = $position;
                // Increment number of shortcodes added to the post.
                $n++;
            }
            // Increase the position of later shortcodes by the length of the current shortcode.
            foreach ( $paragraph_positions as $j => $pp ) {
                if ( $j > $i ) {
                    $paragraph_positions[ $j ] = $pp + strlen( $shortcode );
                }
            }
        }
        $i++;
    }
}

当数字是 4 和 6 而不是 4 和 10 时,我认为这可以正常工作,但是当我开始调整值时,错误变得清晰起来。它目前每10段插入一个广告,当然它不会在第四段之后插入一个广告。

当然,我不反对为此使用explode 或其他方法,但这是让我走得最远的原因。

【问题讨论】:

  • 旁注: 在您发布的代码中,; 缺少 $start_embed_id = 'x100'
  • “而且第一个广告应该出现在这么多段落之后”是主要问题,对吧?
  • @Jeff 谢谢,我太急于清理东西了。但是,是的,主要问题是这可能段落之后的第一个广告。
  • 那么如果第一个广告在第 4 段之后,那么第二个广告是在第 10 段之后还是第 14 段之后?
  • @Anthony 如果第一个广告在第 4 段之后,第二个将在第 14 段之后。

标签: php html string


【解决方案1】:

为什么不将段落拆分成一个数组,添加广告,然后再将它们组合在一起。 它将消除大部分字符串连接头痛。

请注意我没有实现短代码的格式。

$insert_every_paragraphs = 2;
$minimum_paragraph_count = 1;
$adcodes = ['x100', 'x101', 'x102', 'x103'];

$paragraphs = [];
$split = explode('</p>',$content);
foreach($split as $paragraph){
  //filter out empty paragraphs
  if(strlen($paragraph)>3)
      $paragraphs[] = $paragraph . '</p>';
}

$paragraph_count = count($paragraphs);
$ad_num = 0;

$counter = $minimum_paragraph_count;
for($i=0;$i<$paragraph_count;$i++){
    if($counter==0){
        $adcode = $adcodes[$ad_num];
        $shortcode = "[cms_ad:$adcode]";

        array_splice($paragraphs,$i+$ad_num,0,$shortcode);
        $counter = $insert_every_paragraphs;
        $ad_num++;

        if($ad_num>=count($adcodes))
            break;
    }
    $counter--;
}


$content = implode('',$paragraphs);

【讨论】:

  • 我认为这可能会起作用,除非 $maximum_ads 变量是动态的,具体取决于内容的长度。我是不是误会了?
  • 我突然想到我可以像这样计算最大广告数:$paragraph_count = count( $paragraphs );$maximum_ads = floor( ( $paragraph_count - $minimum_paragraph_count ) / $insert_every_paragraphs ) + $minimum_paragraph_count; 这看起来合理吗?
  • @JonathanStegall 我想我不明白您所说的最大广告数量是基于内容长度的动态是什么意思。在您的帖子中,您说“一旦到达 #3 的末尾”,这基本上意味着 4 个广告?
  • 哦,对不起。当我说#3 时,我指的是列表中的第三个要点。我在那里不清楚。基本上,有一个广告代码列表(x100、x101、x102、x103,最多 x110)。这是我没有好好表达的。内容应包含尽可能多的广告,基于$minimum_paragraph_count$insert_every_paragraphs。因此,如果最小值为 4 并且插入间隔为 10,则在第 4 段之后会有一个广告,然后每 10 段有一个广告,直到它遍历完整列表,或者直到内容完成。
  • @JonathanStegall 好的,现在我明白了。 sn-p 已更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-12
  • 1970-01-01
相关资源
最近更新 更多