[英]Php regex - remove anything between $ and line break but leave the break
好的,我有一个php循环,它会生成如下字符串:
$string = "1st fee - $500.00<BR>
2nd fee - $500.00<BR>
3rd fee - $750.00<BR>"
我想删除美元金额,但不确定是否有可能。 我一直在尝试使用preg_replace删除$
到<
之间的所有内容,包括$
但不删除<
以获得类似这样的信息:
$string = "1st fee - <BR>
2nd fee - <BR>
3rd fee - <BR>"
对于我来说不幸的是,我不得不离开-
因为有时在同一行上会有-
多个实例,如下所示:
$string = "1st fee - Brian - $500.00<BR>
2nd fee - John - $500.00<BR>
3rd fee - Bob - $750.00<BR>"
任何帮助将不胜感激,即使只是告诉我放弃也是不可能的。 提前致谢!
因此,基本上,您只想匹配- $
到<br>
的第一个左箭头的所有字符。 模式的第一部分- \\\\$
(由于是regex关键字,因此必须转义$)将与您要删除的内容的开始部分匹配,然后[^<]*
将使用所有内容,直到找到左箭头。
$string = "1st fee - Brian - $500.00<BR>
2nd fee - John - $500.00<BR>
3rd fee - Bob - $750.00<BR>";
$replaced = preg_replace("# - \\$[^<]*#", "", $string);
输出:
string '1st fee - Brian<BR>
2nd fee - John<BR>
3rd fee - Bob<BR>'
您可以使用以下示例删除金额和最后一个-
<?php
$string = "1st fee - $500.00<BR>
2nd fee - $500.00<BR>
3rd fee - Bob - $750.00<BR>";
$pattern = '/\- \$[0-9]+\.[0-9]+\<BR>$/m';
echo preg_replace($pattern, '', $string);
请注意,由于使用了/m
修饰符,该模式与多行匹配,其中$
(在模式的末尾)与一行的末尾匹配。 通过这种方式,可以从右到左和去除量的每一行,只有最后一场比赛-
在里面。
我认为您应该使用完整格式。 您要替换一个金额,因此您应该使用与金额相对应的正则表达式。
$reg = '/ - \$[0-9]+\.[0-9]{2}/';
$withoutAmount = preg_replace($reg, '', $string);
您可以使用其他货币来改善正则表达式:
$reg = '/ - (\$|£|€)[0-9]+\.[0-9]{2}/':
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.