我在学习《PHP与MySQl Web设计第三版》中学习到屏幕提取技术,教材中列举了如下脚本示例以提取亚玛逊股票价格,我改动了一个地方:
—————————————————————————
<html>
<head>
<title>Stock Quote from NASDAQ</title>
</head>
<body>
<?php
//choose stock to look at
$symbol='AMZN';
echo "<h1>Stock Quote for $symbol</h1>";$theurl="http://www.amex.com/equities/listCmp"."/EQLCDetQuote.jsp?Product_Symbol=$symbol";
if (!($contents= file_get_contents($theurl)))
{
  echo 'Could not open URL';
  exit;
}
  //find the part of the page we want and output it
  $pattern='(\\$[0-9]+\\.[0-9]+)';
  
  if (eregi($pattern,$contents,$quote))
  {
    echo "<p>$symbol was last sold at:<br />";
    //echo $quote[1];/*这是教材的脚本原语句是显示数组中第二个元素,我现在是想查看数组的全部元素,所以修改成如下循环语句*/
foreach($quote as $price)
echo $price.'<br/>';
echo '</p>';
  }
  else
  {
    echo '<p>No quote available</p>';
  }
  
  //acknowledge source
  
  echo '<p>'.'This information retrieved from<br />'."<a href=\"$theurl\">$theurl</a><br />".'on '.(date('l jS F Y g:i a T')).'</p>';
?>
</body>
</html>
————————————————————————————————————
脚本执行结果如下:
Stock Quote for AMZN
AMZN was last sold at:
$85.68
$85.68
This information retrieved from
http://www.amex.com/equities/listCmp/EQLCDetQuote.jsp?Product_Symbol=AMZN
on Thursday 4th June 2009 9:03 am CST
——————————————————————————————然而我打来引用的URL地址查看网页内容,发现有8处与正则表达式匹配呀,为何数组只包含两个元素呢?这就是我的疑问,请达人指教。

解决方案 »

  1.   

    没人回答,我自己回答算了,我看了一个函数ereg(),似乎明白了它的意思,它返回的是一个匹配和它的子匹配的数组。而不是所有匹配的集合。我起初以为它返回的是所有匹配的集合。因为脚本中使用了一个括号,所以它只有一个子匹配,所以数组返回两个元素。第一个元素是匹配本身,每二个元素是子匹配,因为脚本将整个pattern括起来了。
      

  2.   

    何为屏幕提取?貌似是JS的东西吧。。PHP只负责服务器端的。你那个是页面的吧。
      

  3.   

    何为屏幕提取?貌似是JS的东西吧。。PHP只负责服务器端的。你那个是页面的吧。
    ____________________________________________________________________
    是的,查看$contents的内容也就是页面在客户端看到的源代码。
      

  4.   

    我现在对这句匹配模式又有些糊涂:
    $pattern='(\\$[0-9]+\\.[0-9]+)';
    它使用了两个反斜框来转义美元符合和小数点,按我现在的知识理解,这样的匹配岂不成匹配一个反斜杠和一个结束符,以及一个反斜杠和一个非换行符外的任何字符?实际上我改成只要一个反斜框来转义就行了,执行效果相同,如下:
    $pattern='(\$[0-9]+\.[0-9]+)';所以,倒底如何理解用两个反斜杠来转义字符呢?
      

  5.   

    file_get_contents這個函數是PHP干的活
      

  6.   

    看php手册上好像推荐的是PCRE的正则表达式,ereg()?好像是POSIX标准的,二种表达式有区别的,而且javascript 端的正则表达式也很类似于PCRE的,只是没有php的强大.PHP also supports regular expressions using a Perl-compatible syntax using the PCRE functions. Those functions support non-greedy matching, assertions, conditional subpatterns, and a number of other features not supported by the POSIX-extended regular expression syntax.