根据段落内容自动插入图片php版

每篇内容插入三张图判断代码附上:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
$chatd="<table>";
    if(stripos($content,$chatd)==0){
    //随机输出三张图功能
    if($moduleid!=37 &&$thumb==''){
    //判断是否存在图
    $id=rand(1,999999);
    $mid=rand(1,9999999);
    $getimg="http://www.nongpin88.com/randimage/".$moduleid."/".$itemid.".png";
    $getimgs="http://www.nongpin88.com/randimage/".$moduleid."/".$id.".png";
    $getimgss="http://www.nongpin88.com/randimage/".$moduleid."/".$mid.".png";
    $count = substr_count($content,'</p>');//获取p标签的数量
      $j = 0;//</p>重新定位用的
      //判断多少个p
      if($count<9){
       $num = 0;//这里是在1/3的位置插入,可以修改为rand(1,$count);这样就是随机位置插入了
       if($count<6){
        $numone=null;
       $numtwo=null;  
        }else{
       $numone=($count-1);
       $numtwo=null;
        }
      }
      elseif($count>8 && $count<12){
        $num = 1;
        $numone=6;
        $numtwo=9;
      }else{
        $num = 1;
        $numone=floor($count/2);
        $numtwo=($count-1);
      }
      for($i = 0; $i < $count; $i++){
         $j = strpos($content, '</p>', $j);
 
         if($i == $num){
          $content = substr($content, 0, $j) . ('<p><img src="' .$getimg .'" /></p>') . substr($content, $j); 
         }elseif($i==$numone){
           $content = substr($content, 0, $j) . ('<p><img src="' .$getimgs .'" /></p>') . substr($content, $j);
        }elseif($i==$numtwo){
           $content = substr($content, 0, $j) . ('<p><img src="' .$getimgss .'" /></p>') . substr($content, $j); 
        }
 
        $j = ($j+1);
      }
    }
 
    }

  这样就ok了,根据段落自动插入图片

效果:

效果演示url:http://www.nongpin88.com/fanwen/3140472.html

posted @   圆柱模板  阅读(83)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
历史上的今天:
2021-09-26 ubuntu多版本php如何切换
2021-09-26 PHP常见安全问题及解决方法
2021-09-26 PHP中16个高危函数
2019-09-26 Discuz! X3 数据字典
2019-09-26 闲谈关于discuz内核缓存机制
2019-09-26 discuz! X3.4特殊字符乱码解决方案
点击右上角即可分享
微信分享提示