douxin5953 2014-08-14 16:33
浏览 47
已采纳

使用preg_replace_callback PHP获取第一个图像后的Add块

I am trying to get add block after 1st image, so far i have mange to get it after 1st paragraph .

Here is my code

$HPSB ='  <img src="**/facebook131update1.png" border="0" alt=""/>
    <p>        
    If you're a Facebook for iOS user and you checked the Updates tab in your App Store application recently, then you probably noticed the new Facebook version 13.1 update that was waiting there for you to install.
    </p>
    Facebook version 13.1 doesn't go very specific into what's new apart from "Bug Fixes," but according to Facebook's detailed ';


function callback_func($matches)
{
  static $count = 0;
  $ret = $matches[1];
  if (++$count == 1)
    $ret .= '<div style="float:left;margin:10px 10px 10px 0;">
                <script type="text/javascript"><!--
        google_ad_client = "ca-pub-***";
        /* iMac 336&#42;280 */
        google_ad_slot = "***";
        google_ad_width = 336;
        google_ad_height = 280;
        //-->
        </script>
        <script type="text/javascript"
        src="//pagead2.googlesyndication.com/pagead/show_ads.js">
        </script>
                </div>';
  return $ret;
}

$content = preg_replace_callback('#(<p>.*?</p>)#', 'callback_func', $HPSB);

How can get it, Please help

EDIT

$HPSB =
    <p style="text-align: center;">
    <img class="size-large wp-image-566952 aligncenter" title="Here’s more evidence Apples ‘iPhone 6' event will be on Sept. 9" src="http://abcaa.com/wp-content/uploads/2014/08/btsendate.jpg" alt="Here’s more evidence Apples ‘iPhone 6' event will be on Sept. 9"/>
    </a>
    </p>
    <p>Weve found an interesting piece of information that further backs up the recent report that Apple is planning to introduce the iPhone 6 at a special media event on Tuesday, Sept 9.</p>
    <p>Apple’s Back to School promotion will also stop on that same date. </p>
    <p>So it definitely makes sense that Apple doesnt want students being able to take advantage of the promotion when buying a new device.</p>
    <p>With this years promotion, college students can receive a $100 Apple Store gift card with the purchase of a Mac or a $50 store gift card when they purchase an iPad or iPhone. Students can also take advantage of Apples educational pricing.</p>
    <p>Along with college students, the promotion is available for parents of a student or a faculty or staff member from any grade level.</p>
    <p>The next-generation handset is expected to be unveiled in both a 4.7-inch and 5.5-inch version. It should also feature a faster A8 processor, a better camera, and other improvements.</p>
    <p>For other recent news, see: Read the entire Tom Hanks interview with the App Store on Twitter, all about apps.</p>
    <br/>
  • 写回答

1条回答 默认 最新

  • dongwen7283 2014-08-14 16:51
    关注

    I'm not completely sure I've got the question but you can get (and pass to callback_func) the text inside the p tag with:

    $content = preg_replace_callback("#<p>(.*?)<\/p>#is", 'callback_func', $HPSB);
    

    ----EDIT-----

    With the snippet I wrote you, you're not getting the content after 1st <p> but exclusively the content enclosed between its tags. If you want to get after 1st <img, you need:

    $content = preg_replace_callback("#<img.*/>(.*)#is", 'callback_func', $HPSB);
    

    This way you'll get everything after the img tag.


    ----------EDIT---------- (the last I swear)

    1) enclosed between p tags:

    $content = preg_replace_callback("#<p>(.*?)<\/p>#is", 'callback_func', $HPSB);
    

    result ($matches[1]):

    If you're a Facebook for iOS user and you checked the Updates tab in your App Store application recently, then you probably noticed the new Facebook version 13.1 update that was waiting there for you to install.
    ' (length=222)
    

    2) after img tag

    $content = preg_replace_callback("#<img.*/>(.*)#is", 'callback_func', $HPSB);
    

    result ($matches[1]):

    <p> 
    If you're a Facebook for iOS user and you checked the Updates tab in your App Store application recently, then you probably noticed the new Facebook version 13.1 update that was waiting there for you to install.
    </p>
    Facebook version 13.1 doesn't go very specific into what's new apart from "Bug Fixes," but according to Facebook's detailed ' (length=363)
    

    3) Now you ask the first <img, so:

    $content = preg_replace_callback("#(<img.*/>)#is", 'callback_func', $HPSB);
    

    result ($matches[1]):

    '<img src="**/facebook131update1.png" border="0" alt=""/>' (length=56)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题