dpba63888 2010-07-12 23:50
浏览 64
已采纳

php preg_match_all,正则表达式,提取特定值

I'm trying to extract the users who ask questions on a classified ad website(http://trademe.co.nz/Trade-Me-Motors/Cars/Toyota/Hiace/auction-300294634.htm) . For some reasons the pattern which I'm using is not working always so I would appreciate if you will help me with a perfect regex Here is my current code

/get memberid of the question asker
   $pattern = "//m";
   preg_match_all($pattern, $htmlContent, $member_match);

   $no_a = count($member_match[1];);            

   $inc = 0;

  echo "number of askers is $no_a";

  //make loop to get all the members
     while($inc ";
//get member user match based on the member_id
      $pattern2 = "/(.*)/";
     preg_match_all($pattern2, $htmlContent, $member_user_match);
      $bid_user_q = $member_user_match[1][0];
//store the askers
    mysql_query("INSERT INTO askers (id, item_number, bid_user_q, bid_member_id_q, sub_cat)
                             VALUES('', '$item_number', '$bid_user_q', '$bid_member_id_q', '$sub_cat')");
echo "INSERT INTO askers (id, item_number, bid_user_q, bid_member_id_q)
                             VALUES('', '$item_number', '$bid_user_q', '$bid_member_id_q', '$sub_cat')
"; mysql_error(); $inc++; }

The code doesn't seem to be displayed properly due the html tags from pattern so you can see it here http://pastebin.com/iPxizy5X

  • 写回答

1条回答 默认 最新

  • dsxz84851 2010-07-13 00:05
    关注

    I doubt it is "perfect", but this one worked for me:

    /<small>\s*<a href=\"\/Members\/Listings\.aspx\?member=(\d+)\">\s*<b>(.*?)<\/b>/
    

    If you use:

    $pattern = "/<small>\s*<a href=\"\/Members\/Listings\.aspx\?member=(\d+)\">\s*<b>(.*?)<\/b>/";
    preg_match_all($pattern, $htmlContent, $member_match, PREG_SET_ORDER);
    

    $member_match[0][1] = member id $member_match[0][2] = member nick $member_match[1][1] = member id $member_match[1][2] = member nick

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了
  • ¥15 微信小程序商城如何实现多商户收款 平台分润抽成