How should I write a regular expression that matches within an area?

Source: Internet
Author: User
  has a $link= "url", open after the code as follows: ...            
   
   
   
  • 2016-06-06 Yang Shan 2016 14 road greening project tender announcement
  • 2016-06-06 Pingqiao District Minggang Town intersection-waste treatment yard-Hegang village South Junction (k0+000-k4+300) Road reconstruction project construction tender Notice
  • 2016-05-31 Yang Shan branch of Xinyang City Center Hospital Public Defense Project Supervision tender notice
  • 2016-05-31 Xinyang Hospital ward Building construction project construction and supervision Project tender announcement
!--Paging--> ...
目标:获得......中href标签里的内容"/news!show.action?id=2a7060f3519b40b3aa766dd53f2b00ad"我写的代码如下://获取链接中的内容$htmlContent=file_get_contents("$link");//获取链接$num=preg_match_all("/.*?(href=\".*?\").*/is",$htmlContent,$array);//打印数组var_dump($array[1]);目前卡顿的地方:在  //获取链接  处,子表达式只能获取一个结果,而且不是想要的href标签中的内容。

Reply content:

  has a $link= "url", open after the code as follows: ...            
   
   
   
  • 2016-06-06 Yang Shan 2016 14 road greening project tender announcement
  • 2016-06-06 Pingqiao District Minggang Town intersection-waste treatment yard-Hegang village South Junction (k0+000-k4+300) Road reconstruction project construction tender Notice
  • 2016-05-31 Yang Shan branch of Xinyang City Center Hospital Public Defense Project Supervision tender notice
  • 2016-05-31 Xinyang Hospital ward Building construction project construction and supervision Project tender announcement
!--Paging--> ...
目标:获得......中href标签里的内容"/news!show.action?id=2a7060f3519b40b3aa766dd53f2b00ad"我写的代码如下://获取链接中的内容$htmlContent=file_get_contents("$link");//获取链接$num=preg_match_all("/.*?(href=\".*?\").*/is",$htmlContent,$array);//打印数组var_dump($array[1]);目前卡顿的地方:在  //获取链接  处,子表达式只能获取一个结果,而且不是想要的href标签中的内容。

Look around should be able to meet your current needs.

preg_match_all('/(?<=\d{4}-\d{2}-\d{2}<\/span>)(?:.*?href=\")(.*?)(?:\".*?)(?=<\/li>)/is', $a, $matches);
array(2) {  [0]=>  array(4) {    [0]=>    string(152) "              羊山新区2016年14条道路绿化工程招标公告              "    [1]=>    string(218) "              平桥区明港镇井庄路口-垃圾处理场-何岗村南路口(K0+000-K4+300)公路改建工程施工招标公告              "    [2]=>    string(161) "              信阳市中心医院羊山分院人防工程监理招标公告              "    [3]=>    string(173) "              信阳市儿童医院病房楼建设项目施工及监理项目招标公告              "  }  [1]=>  array(4) {    [0]=>    string(53) "/news!show.action?id=f435345c44e04ec3a5e6ccedca29e061"    [1]=>    string(53) "/news!show.action?id=ad4b065149d94704b3d295287f863b5a"    [2]=>    string(53) "/news!show.action?id=c3b688ae2ec54fb0880a0f60f7a4f5f0"    [3]=>    string(53) "/news!show.action?id=2a7060f3519b40b3aa766dd53f2b00ad"  }}

Cause: Only one match result of course the subexpression has only one result.

If you change to:

$num = preg_match_all("/.*?(href=\".*?\").*?/is", $htmlContent, $array);

There are four match results for 4 matches.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.