2017-01-01 43 views
-2

如何獲得只有數字0-9和+從以下變量最好的辦法讓只有數字0-9和+從變量

$phone = 「No +93-332-1121545」; 

可能有一些空格和其他字符的變量我不需要。

我只需要

$cleanPhone = 「+933321121545」; 
+0

'preg_replace(「/ [^ 0-9 +] /」,「」,'No + 93-332-1121545')' – Federkun

回答

1

使用preg_replace()刪除不是你想要的那些以外的任何字符。

$cleanPhone = preg_replace('/[^\d+]/', '', $phone);