我想使用這個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
運行此腳本時獲取輸出的內容是什麼?有可能雅虎返回與您的腳本不同的東西,而不是返回瀏覽器。 –