我想使用这个PHP脚本,以查找从雅虎财经股票报价。我遇到的问题是脚本运行时,它不会产生任何结果。这使我相信我的正则表达式是不正确的,但是当我在myregextester.com上使用相同的正则表达式时,它显示了我期望给定输入的结果。任何帮助将不胜感激。此外,我的PHP可能是不正确的,我想要做的。php与preg_match_all()生成没有结果
<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>";
//echo 'this printed (1)<br />';
$theurl = 'http://finance.yahoo.com/q?s=AMZN';
//echo 'this printed (2)<br />';
$contents = file_get_contents($theurl);
//find the part of the page we want and output it
if (preg_match_all('/amzn">([0-9]+\.[0-9]+)/', $contents, $matches)) {
echo "The price for $symbol: ".$matches[1][0];
} else {
echo "No Results";
}
?>
</body>
</html>
哥们,当涉及到正则表达式,总是带着小文的例子开始并测试了很多。 – Rufus
运行此脚本时获取输出的内容是什么?有可能雅虎返回与您的脚本不同的东西,而不是返回浏览器。 –