+ -
当前位置:首页 → 问答吧 → 寻求高手讲解下如何使用preg_match和preg_match_all

寻求高手讲解下如何使用preg_match和preg_match_all

时间:2010-09-14

来源:互联网

前两天论坛一位高手告诉我,由于eregi在PHP后续版本中不再支持,建议我使用preg_match和preg_match_all.
我看了半天手册,对比eregi,我感觉无法理解和运用。
所以求教高手们,以下是我的一个实例,麻烦高手将其中的eregi语句转换成preg_match或preg_match_all,也能正常实现我所要的功能
小弟在此谢过!
复制代码
  1.  
  2. <?php
  3. error_reporting(E_ALL^E_NOTICE);
  4. $strContent  = file_get_contents("http://www.darenwangluo.com.cn/fly.asp?gid=2");
  5. eregi("<table width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#E3E3E3\">(.*)</table>",$strContent,$regs);
  6. $b = explode("\"#FFFFFF\">",$regs[0]);
  7. echo "<table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#E3E3E3\">\n";
  8. for($i=2;$i<=1433;$i=$i+3){
  9.  eregi("(.*)</td>",$b[$i],$c);
  10.  eregi("(.*)</td>",$b[$i+1],$d);
  11.  echo "<tr><td width=\"350\">".$c[1]."</td><td width=\"150\">".$d[1]."</td></tr>\n";
  12. }
  13. echo "</table>";
  14. ?>
  15.  


作者: amwuetk   发布时间: 2010-09-14

起码代码应该分行,分段吧,这么乱,没人愿意看

作者: kuaiman1   发布时间: 2010-09-14

不好意思啊,前面我看都是好好的,真是抱歉,我重新修改了一下

作者: amwuetk   发布时间: 2010-09-14

解决了
复制代码
  1. error_reporting(E_ALL^E_NOTICE);
  2. $strContent  = file_get_contents("http://www.darenwangluo.com.cn/fly.asp?gid=2");
  3. preg_match('|<table width=\"100%\" border=\"0\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#E3E3E3\">(.*)</table>|s',$strContent,$regs);
  4. $b = explode("\"#FFFFFF\">",$regs[0]);
  5. echo "<table width=\"500\" border=\"0\" align=\"center\" cellpadding=\"4\" cellspacing=\"1\" bgcolor=\"#E3E3E3\">\n";
  6. for($i=2;$i<=1433;$i=$i+3){
  7. preg_match('|(.*)</td>|s',$b[$i],$c);
  8. preg_match('|(.*)</td>|s',$b[$i+1],$d);
  9. echo "<tr><td width=\"350\">".$c[1]."</td><td width=\"150\">".$d[1]."</td></tr>\n";
  10. }
  11. echo "</table>";
[ 此帖被kuaiman1在2010-09-14 22:25重新编辑 ]

作者: kuaiman1   发布时间: 2010-09-14

热门下载

更多