雷格斯,赛前


Regex, preg match

好的,我想从网站上读一张表(汇率)我正在尝试从以下行读取一些数据:

<table width="100%" border="0" 

我得到了上面几行的副本,用下面几行我可以从我放置该表副本的地址中读取我想要的数据。

$content = file_get_contents("http://www.example.com/");
preg_match('#">([0-9,.]*)<.*">([0-9,.]*)<#Uis', $content, $USDmatch);
preg_match('#">([0-9,.]*)<.*">([0-9,.]*)<#Uis', $content, $EURmatch);
preg_match('#">([0-9,.]*)<.*">([0-9,.]*)<#Uis', $content, $GBPmatch);
$eur = $EURmatch[2];
$usd = $USDmatch[2];
$gbp = $GBPmatch[2];
echo "EUR: $eur USD: $usd GBP: $gbp";

但同样的行,我无法从源代码中读取该表!你能建议我找出我的错误吗?

感谢

通过更多的交换,发现冗余规范的一些细节导致了不匹配。

送上以下更换件。它会起作用:

preg_match('#MonetaryCode">USD.*MonetaryBuy">([0-9,.]*)<.*MonetarySell">([0-9,.]*)<#Uis', $content, $USDmatch);
preg_match('#MonetaryCode">EUR.*MonetaryBuy">([0-9,.]*)<.*MonetarySell">([0-9,.]*)<#Uis', $content, $EURmatch);
preg_match('#MonetaryCode">GBP.*MonetaryBuy">([0-9,.]*)<.*MonetarySell">([0-9,.]*)<#Uis', $content, $GBPmatch);
$eur = $EURmatch[2];
$usd = $USDmatch[2];
$gbp = $GBPmatch[2];

我只是缩短了regexp并消除了不必要的分组。关于贪婪和底线突破的言论都是错误的。您已经有了适当的修饰符,而我刚刚在长rexep模式字符串中忽略了这些修饰符。很抱歉造成混乱。