我需要在regex php中提供帮助。 如果在字符串中找到某个字符后的数字。获取该数字并在应用数学后替换它。像货币兑换一样。
([^ \?] )AUD(\ d )
正则表达式不正确我希望所有匹配的数字只在这里匹配40但是还有20.00,9.9等。我想要全部。转换它们。
ondeviceorientation
答案 0 :(得分:3)
如果您只需要获取所有这些值并使用simpleConvert
转换它们,请使用正则表达式获取整数/浮点数,并在获取值后将数组传递给array_map
:
$pattern_new = '/\bAUD (\d*\.?\d+)/';
preg_match_all($pattern_new, $content, $vals);
print_r(array_map(function ($a) { return simpleConvert("AUD", "USD", $a); }, $vals[1]));
请参阅this PHP demo。
模式详情:
\b
- 领先的单词边界AUD
- 文字字符序列
- 空格(\d*\.?\d+)
- 第1组捕获0+位数,可选.
,然后是1位数。请注意,传递给$m[1]
函数的simpleConvert
包含第一个(也是唯一的)捕获组的内容。
如果您想在输入文本中更改这些值,我建议使用preg_replace_callback
中的相同正则表达式:
$content = "The following fees and deposits are charged by the property at time of service, check-in, or check-out.\r\n\r\nBreakfast fee: between AUD 9.95 and AUD 20.00 per person (approximately)\r\nFee for in-room wireless Internet: AUD 0.00 per night (rates may vary)\r\nFee for in-room high-speed Internet (wired): AUD 9.95 per night (rates may vary)\r\nFee for high-speed Internet (wired) in public areas: AUD 9.95 per night (rates may vary)\r\nLate check-out fee: AUD 40\r\nRollaway beds are available for an additional fee\r\nOnsite credit card charges are subject to a surcharge\r\nThe above list may not be comprehensive. Fees and deposits may not include tax and are subject to change.";
$pattern_new = '/\bAUD (\d*\.?\d+)/';
$res = preg_replace_callback($pattern_new, function($m) {
return simpleConvert("AUD","USD",$m[1]);
}, $content);
echo $res;
请参阅PHP demo