dshgdhdfcas30210 2013-12-16 03:36
浏览 45
已采纳

找出正确的preg_match正则表达式匹配

I am curling a page with php and then I am looking to find a section within that page. That section opens and closes with the html5 <section> tag like this:

<section id="postingbody">
   blah blah blah content
</section>

I am not sure how to get my matching working properly. Just to fill in the matching portion here:

preg_match("/ id=\"postingbody\"\">???????<\/section>/i", $compiled_results, $matches2);

Edit

So here is an example section of the content

<section id="postingbody">
    Looking to find a side job ( working your own hours ) or career in the new media field & internet marketing? Web design, graphic design, SEO, Printing & Internet marketing company looking to hire a sales team member. We have 10+ years experience in the Web design & marketing field. Work your own hours, competitive commission rates, we can also train the right candidates for sales. Our office is located in New Jersey.<br>
</section>

So the examples here don't seem to work.

  • 写回答

2条回答 默认 最新

  • dsutuyxe088689 2013-12-16 03:39
    关注

    Try this:

    preg_match("/(?s)<section id=\"postingbody\">((?:.)*?)<\/section>/i", $compiled_results, $matches2);
    

    Regular expression visualization

    Debuggex Demo

    Edit: For example, the following code works as expected for me (the value is in $matches2):

    $compiled_results = '<section id="postingbody">
        Looking to find a side job ( working your own hours ) or career in the new media field & internet marketing? Web design, graphic design, SEO, Printing & Internet marketing company looking to hire a sales team member. We have 10+ years experience in the Web design & marketing field. Work your own hours, competitive commission rates, we can also train the right candidates for sales. Our office is located in New Jersey.<br>
    </section>';
    preg_match("/(?s)<section id=\"postingbody\">((?:.)*?)<\/section>/i", $compiled_results, $matches2);
    var_dump($matches2);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备