将内容放置在没有图像的段落之间 [英] Place content in between paragraphs without images

查看:104
本文介绍了将内容放置在没有图像的段落之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <?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。'...';
回显'您的ADS代码';
echo $ second_half_content;
?>

如何修改此内容以使广告代码的2段(顶部和底部)不应该是具有图像的人。如果顶部或底部段落有图像,请尝试下两个段落。



示例:右侧正确执行。 https://i.stack.imgur.com/66D2C.pngalt =在这里输入图片描述>

解决方案

preg_replace版本



这段代码遍历每一段,忽略那些包含图像标签的段落。如果遇到图像, $ pcount 变量对于没有图像的每个段落都是递增的,但是, $ pcount 是重置为零。一旦 $ pcount 达到了它会达到2的点,广告标记就会被插入到该段落之前。这应该使广告标记在两个安全段落之间。广告标记变量将被取消,因此只插入一个广告。



以下代码仅用于设置,可以修改为以不同方式拆分内容,您也可以修改使用的正则表达式—

  ///设置我们的广告内容
$ advert ='< marquee>购买这个东西!!< / marquee>'。 \\\
\\\
;
///计算中点
$ mpoint = floor(strlen($ content)/ 2);
///修改回到段落的开头
$ mpoint = strripos($ content,'< p', - $ mpoint);
/// split html,所以我们只在后半部分工作
$ first = substr($ content,0,$ mpoint);
$ second = substr($ content,$ mpoint);
$ pcount = 0;
$ regexp ='/<p>.+?<\/p>/si';

其余部分是运行替换的大部分代码。这可以修改插入多个广告,或支持更多涉及的图像检查。

  $ content = $ first。 preg_replace_callback($ regexp,function($ matches){
global $ pcount,$ advert;
if(!$ advert){
$ return = $ matches [0];
}
else if(stripos($ matches [0],'< img')!== FALSE){
$ return = $ matches [0];
$ pcount = 0;
}
else if($ pcount === 1){
$ return = $ advert。$ matches [0];
$ advert ='';
}
else {
$ return = $ matches [0];
$ pcount ++;
}
return $ return;
},$ second);

执行完这段代码后, $ content 变量将包含增强的HTML。






5.3之前的PHP版本



由于您选择的测试区域不支持PHP 5.3,因此不支持匿名函数,因此您需要使用稍微修改且不太简洁的版本;而是使用一个命名函数。



另外,为了支持可能实际上没有为广告留下空间的内容,我在下半部分修改了 $ mpoint ,以便从最后计算为80%。这会在 $ second 部分中包含更多的效果 - —但也意味着您的广告通常会高于标准价格。这段代码没有实现任何回退,因为你的问题没有提到发生故障时会发生什么。

  $ advert ='< marquee>购买这个东西!!< / marquee>'。 \\\
\\\
;
$ mpoint = floor(strlen($ content)* 0.8);
$ mpoint = strripos($ content,'< p', - $ mpoint);
$ first = substr($ content,0,$ mpoint);
$ second = substr($ content,$ mpoint);
$ pcount = 0;
$ regexp ='/<p>.+?<\/p>/si';

函数replacement_callback($ matches){
global $ pcount,$ advert;
if(!$ advert){
$ return = $ matches [0];
}
else if(stripos($ matches [0],'< img')!== FALSE){
$ return = $ matches [0];
$ pcount = 0;
}
else if($ pcount === 1){
$ return = $ advert。 $比赛[0];
$ advert ='';
}
else {
$ return = $ matches [0];
$ pcount ++;
}
return $ return;
}

先回声$。 preg_replace_callback($ regexp,'replacement_callback',$ second);


I am using the following code to place some ad code inside my content .

<?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;
?>

How can i modify this so that the 2 paragraphs (top and bottom) enclosing the ad code should not be the one having images. If the top or bottom paragraph has image then try for next 2 paragraphs.

Example: Correct Implementation on the right.

解决方案

preg_replace version

This code steps through every paragraph ignoring those that contain image tags. The $pcount variable is incremented for every paragraph found without an image, if an image is encountered however, $pcount is reset to zero. Once $pcount reaches the point where it would hit two, the advert markup is inserted just before that paragraph. This should leave the advert markup between two safe paragraphs. The advert markup variable is then nullified so only one advert is inserted.

The following code is just for set up and could be modified to split the content differently, you could also modify the regular expression that is used — just in case you are using double BRs or something else to delimit your paragraphs.

/// set our advert content
$advert = '<marquee>BUY THIS STUFF!!</marquee>' . "\n\n";
/// calculate mid point
$mpoint = floor(strlen($content) / 2);
/// modify back to the start of a paragraph
$mpoint = strripos($content, '<p', -$mpoint);
/// split html so we only work on second half
$first  = substr($content, 0, $mpoint);
$second = substr($content, $mpoint);
$pcount = 0;
$regexp = '/<p>.+?<\/p>/si';

The rest is the bulk of the code that runs the replacement. This could be modified to insert more than one advert, or to support more involved image checking.

$content = $first . preg_replace_callback($regexp, function($matches){
  global $pcount, $advert;
  if ( !$advert ) {
    $return = $matches[0];
  }
  else if ( stripos($matches[0], '<img ') !== FALSE ) {
    $return = $matches[0];
    $pcount = 0;
  }
  else if ( $pcount === 1 ) {
    $return = $advert . $matches[0];
    $advert = '';
  }
  else {
    $return = $matches[0];
    $pcount++;
  }
  return $return;
}, $second);

After this code has been executed the $content variable will contain the enhanced HTML.


PHP versions prior to 5.3

As your chosen testing area does not support PHP 5.3, and so does not support anonymous functions, you need to use a slightly modified and less succinct version; that makes use of a named function instead.

Also, in order to support content that may not actually leave space for the advert in it's second half I have modified the $mpoint so that it is calculated to be 80% from the end. This will have the effect of including more in the $second part — but will also mean your adverts will be generally placed higher up in the mark-up. This code has not had any fallback implemented into it, because your question does not mention what should happen in the event of failure.

$advert = '<marquee>BUY THIS STUFF!!</marquee>' . "\n\n";
$mpoint = floor(strlen($content) * 0.8);
$mpoint = strripos($content, '<p', -$mpoint);
$first  = substr($content, 0, $mpoint);
$second = substr($content, $mpoint);
$pcount = 0;
$regexp = '/<p>.+?<\/p>/si';

function replacement_callback($matches){
  global $pcount, $advert;
  if ( !$advert ) {
    $return = $matches[0];
  }
  else if ( stripos($matches[0], '<img ') !== FALSE ) {
    $return = $matches[0];
    $pcount = 0;
  }
  else if ( $pcount === 1 ) {
    $return = $advert . $matches[0];
    $advert = '';
  }
  else {
    $return = $matches[0];
    $pcount++;
  }
  return $return;
}

echo $first . preg_replace_callback($regexp, 'replacement_callback', $second);

这篇关于将内容放置在没有图像的段落之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆