[英]Regular expression to obtain numbers and decimals
I'm trying to access 我正在尝试访问
1.20163 1.20163
from the following: 从以下内容:
1 British Pound Sterling = 1.20163 Euro 1英镑= 1.20163欧元
So far I have: 到目前为止,我有:
$exchange['rate'] = $xml->channel->item[15]->description;
$rate = preg_match('/^[0-9]{1,}$/', '', $exchange['rate']);
However, this seems to returns 但是,这似乎又回来了
1 British Pound Sterling = 1.20163 Euro 1英镑= 1.20163欧元
Any ideas? 有任何想法吗?
I think you using preg_match wrong You want to extract the value of 1.20163 from the string, right? 我认为您使用的preg_match错误您想从字符串中提取1.20163的值,对吧? Then do this: 然后执行以下操作:
$s = '1 British Pound Sterling = 1.20163 Euro';
preg_match('/([0-9]+\.[0-9]+)/', $s, $matches);
$result = $matches[0];
You got your result in $result. 您获得了$ result的结果。
/^[0-9]{1,}$/
means you want to match a line containing only numbers. 表示您要匹配仅包含数字的行。 You wont get any match in your case. 您不会遇到任何匹配的情况。 You also use preg_match in a wrong way. 您还会以错误的方式使用preg_match。 See the documentation . 请参阅文档 。
Try something like: 尝试类似的方法:
$exchange['rate'] = $xml->channel->item[15]->description;
preg_match('/=\s*([0-9.]+)\s/', $exchange['rate'], $matches);
// the result will be in $matches
$rate = $matches[1];
Try 尝试
'/([0-9]{1,}\.[0-9]{1,}) /'
Match numbers then decimal then numbers then space. 匹配数字,然后是十进制,然后是数字,然后是空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.