2011-11-08 30 views
3

我使用USAePay的[恐怖] PHP庫連接到自己的網關的preg_replace,但我發現了以下錯誤:轉換ereg_replace在USAePay源代碼

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

這相當於下面一行:

​​

所以,我想與preg_replace切換出來。
這裏就是我想:

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

這是等同於一個以上,或不?

回答

3

您想要從表示金額的字符串中刪除任何非數字或非句點字符。你可以這樣做:用

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

正則表達式:

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

'ereg'中的'[:digit:]'相當於'preg'中的簡單'\ d'嗎? – MegaHit

+0

實際上,PCRE也支持[:digit:]字符類(參見[這個鏈接](http://ua.php.net/manual/en/regexp.reference.character-classes.php))。是的,他們都意味着同樣的事 – Exander

+0

你應該使用正則表達式字符串的單引號。否則,你可能會意外地使用一些轉義序列而不是原始的'\ something'。 – ThiefMaster

4

您可能需要下載最新版本的usaepay庫:

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

與替換線問題:

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

''/ [^ \ d。] /''更好(不需要在字符類中跳過點) – ThiefMaster

+0

您喜歡\ d超過0-9的任何特定原因?速度沒有區別,我認爲0-9更加明顯。如果你真的想去\ d路線,爲什麼不做\ d而不是^ \ d。另外,我認爲使用\。儘管模式(包括括號內)是一個好主意。當你在括號外時,錯誤地忘記了斜線,這可能導致意想不到的模式匹配... – nadachicken

+0

個人而言,當我需要角色類時,我更喜歡'0-9' - 但是'\ d'仍然是「更清晰」 IMO。 '\ D'不會因爲你想要允許的'.'而工作。 – ThiefMaster