【发布时间】:2017-05-25 22:03:35
【问题描述】:
我需要 regex php 方面的帮助。 如果在字符串中找到某个字符后的数字。获取该数字并在应用数学后将其替换为。像货币转换一样。
我应用了这个正则表达式https://regex101.com/r/KhoaKU/1
([^\?])澳元 (\d)
正则表达式不正确我想要所有匹配的数字,只有它匹配 40,但也有 20.00、9.95 等。我试图得到所有。并转换它们。
function simpleConvert($from,$to,$amount)
{
$content = file_get_contents('https://www.google.com/finance/converter?a='.$amount.'&from='.$from.'&to='.$to);
$doc = new DOMDocument;
@$doc->loadHTML($content);
$xpath = new DOMXpath($doc);
$result = $xpath->query('//*[@id="currency_converter_result"]/span')->item(0)->nodeValue;
return $result;
}
$pattern_new = '/([^\?]*)AUD (\d*)/';
if ( preg_match ($pattern_new, $content) )
{
$has_matches = preg_match($pattern_new, $content);
print_r($has_matches);
echo simpleConvert("AUD","USD",$has_matches);
}
【问题讨论】:
-
那么,你需要匹配的值是
40?你的正则表达式正确吗? -
到底是什么问题?
-
@WiktorStribiżew 没有正则表达式不正确我想要所有匹配的数字在这里只有匹配 40 但也有 20.00、9.95 等。我试图得到所有。并转换它们。
-
如果你需要全部,你可以使用
AUD ([\d.]+)之类的东西。这也将包括点.。并且您希望preg_match_all()获得所有匹配项。
标签: php regex preg-replace preg-match preg-match-all