阅读背景:

在USAePay源代码中将ereg_replace转换为preg_replace

来源:互联网 

I'm using USAePay's [horrible] PHP library to connect to their gateway, but I'm getting the following error:

我正在使用USAePay的[可怕] PHP库连接到他们的网关,但是我收到以下错误:

Deprecated: Function ereg_replace() is deprecated in .../usaepay.php on line 320

不推荐使用:函数ereg_replace()在第320行的... / usaepay.php中已弃用

This corresponds to the following line:

这对应于以下行:

$this->amount = ereg_replace("[^[:digit:].]", "", $this->amount);

So, I'd like to switch it out with preg_replace.
Here's what I'm thinking:

所以,我想用preg_replace切换它。这就是我的想法:

$this->amount = ereg_replace("/[^[\d].]/", "", $this->amount);

Is this equivalent to the one above, or not?

这相当于上面的那个吗?

2 个解决方案

#1


3  

You want to delete any non-digit or non-period character from the string that represents an amount. You can do:

您希望从表示金额的字符串中删除任何非数字或非句点字符。你可以做:

$this->amount = preg_replace("/[^\d.]/", "", $this->amount);

Regex used:

正则表达式使用:

[   - Start of character class
 ^  - Negation
 \d - any digit
 .  - a literal perios
]   - end of character class

#2


4  

You might want to grab the latest version of the usaepay library:

您可能想要获取最新版本的usaepay库:

https://github.com/usaepay/usaepay-php

https://github.com/usaepay/usaepay-php

which replaces the line in question with:

用以下内容替换有问题的行:

$this->amount=preg_replace("/[^0-9\.]/","",$this->amount);

分享到: