PHP在X段之后插入广告,如果文本足够长,则每隔Y段插入广告
我需要以这种方式每隔一段时间插入一个广告代码:PHP在X段之后插入广告,如果文本足够长,则每隔Y段插入广告,php,html,string,Php,Html,String,我需要以这种方式每隔一段时间插入一个广告代码: $insert_every_段落是一个变量,指示段落在文本中出现的频率。例如,10 $minimum_paragration_count用于查看文本是否足够长,是否适合任何广告,并且第一个广告应该出现在这许多段落之后(然后它应该重复#1中的间隔)。4,例如 广告代码是这样工作的:x100、x101、x102、x103等。用户可以设置可以添加的数量。它们的呈现方式类似于[cms\u ad:x100],PHP稍后会替换它们(该部分工作正常) 代码应该在
$insert_every_段落
是一个变量,指示段落在文本中出现的频率。例如,10$minimum_paragration_count
用于查看文本是否足够长,是否适合任何广告,并且第一个广告应该出现在这许多段落之后(然后它应该重复#1中的间隔)。4,例如[cms\u ad:x100]
,PHP稍后会替换它们(该部分工作正常)$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++;
}
}
$paragration_end='';
$insert_每段=10;
$minimum_段落计数=4;
$embed_前缀='x';
$start_embed_id='x100';
$start_embed_count=intval(str_replace($embed_prefix,,$start_embed_id));//ex100
$end_embed_id='x103';
$end_embed_count=intval(str_replace($embed_prefix,,$end_embed_id));//ex104
$段落位置=数组();
$last_position=-1;
while(stripos($content,$paragration\u end,$last\u position+1)!==false){
//获取下一个$U段末尾的位置。
$last_position=stripos($content,$paragration_end,$last_position+1)+3;
$段落位置[]=$最后位置;
}
//如果段落总数大于最小段落数
如果(计数($段落位置)>=$最小段落计数){
//添加了多少广告?
$n=$start\u embed\u count;
//存储最后一次插入的位置。
$previous\u position=$start\u embed\u count;
$i=0;
而($i$pp){
如果($j>$i){
$paragration_positions[$j]=$pp+strlen($shortcode);
}
}
}
$i++;
}
}
当数字是4和6而不是4和10时,我认为这可以正常工作,但当我开始调整值时,错误变得很明显。它目前每10段插入一则广告,当然它不会在第四段之后插入广告
当然,我并不反对使用分解法或其他方法,但这正是让我发挥最大作用的原因。为什么不将段落拆分成一个数组,添加广告,然后再将其连接在一起呢。 这将消除大部分的字符串浓缩头痛 请注意,我没有实现短代码的格式设置
$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);
$insert\u every\u段落=2;
$minimum_段落计数=1;
$adcodes=['x100','x101','x102','x103'];
$段落=[];
$split=分解(“”,$content);
foreach($拆分为$段){
//过滤掉空段落
如果(斯特伦($段落)>3)
$段落[]=$段落。';
}
$PARATION_count=计数($PARATIONS);
$ad_num=0;
$counter=$minimum\u段落\u计数;
对于($i=0;$i=count($adcode))
打破
}
$counter--;
}
$content=内爆(“”,$段落);
旁注:在您发布的代码中有一个
在$start\u embed\u id='x100'
处缺失,而且第一个广告应该出现在这么多段落之后,“这是主要问题,对吧?@Jeff谢谢,我太渴望清理东西了。但是,是的,主要的问题是在这五个段落之后的第一个广告。因此,如果第一个广告在第4段之后,那么第二个广告是在第10段还是第14段之后?@Anthony如果第一个广告在第4段之后,第二个是在第14段之后。我认为这可能有效,除了$maximum\u ads
变量是动态的,取决于内容的长度。我是不是误解了?我突然想到,我可以这样计算最大广告数:$paragration\u count=count($paragrations)
$maximum\u ads=floor($paragration\u count-$minimum\u paragration\u count)/$insert\u every\u paragrations)+$minimum\u paragration\u count代码>这看起来合理吗?@JonathanStegall我想我不明白你所说的基于内容长度的动态最大广告数量是什么意思。在你的帖子中,你说“一旦到了#3结尾”,这基本上意味着4个广告?哦,对不起。当我说#3时,我指的是我清单中的第三个要点。我在那里不清楚。基本上,有一个ad代码列表(x100、x101、x102、x103,最多x110)。这是我没有说清楚的。根据$minimum\u paragration\u count
和$insert\u every\u paragrams
,内容应包含尽可能多的广告。因此,如果最小值为4,插入间隔为10,则在第4段之后会有一个广告,然后每10段会有一个广告,直到它在完整列表中运行,或者直到内容完成。@JonathanStegall好的,现在我知道了。代码段已更新。