-3

电话号码的正则表达式有一些问题,到目前为止它可以工作,但我不知道如何用空格替换“//”:

输入:0()()()111 1-11* // *1111

预计:: +49 111 111 1111

得到:+49 111 1111111

$pattern = array(
            '/[^0-9\+\.\-\(\) ]/',   // Cut all characters that are not allowed
            '/^00/',                 // Start with 00 ist changed to +
            '/^(0)(\d)/',            // Start with 0[1-9] ist changed to +49 [0-9]
            '/[\.\-\(\)]/',          // Change allowed characters to ' '
            '/\s[\s]+/',             // Change grouped spaces too one
            '/((\+49 )(0))(.*)/'
        );

$change = array('', '+', '+49 $2', ' ', ' ', '$2$4');
$value = preg_replace($pattern, $change, $value);

将字符添加到:

 '/[\.\-\(\)]/',          // Change allowed characters to ' '

这是行不通的。抱歉,我对正则表达式的了解有限。

4

2 回答 2

1

试试这个,我刚刚测试过它并且可以工作。所以首先我要替换双斜杠,然后是不允许的字符。

$pattern = array(
            '/[\/]{2}/',             // Replacing //
            '/[^0-9\+\.\-\(\) ]/',   // Cut all characters that are not allowed
            '/^00/',                 // Start with 00 ist changed to +
            '/^(0)(\d)/',            // Start with 0[1-9] ist changed to +49 [0-9]
            '/[\.\-\(\)]/',          // Change allowed characters to ' '
            '/\s[\s]+/',             // Change grouped spaces too one
            '/((\+49 )(0))(.*)/'
        );

$change = array(' ', '', '+', '+49 $2', ' ', ' ', '$2$4');
$value = preg_replace($pattern, $change, $value);
于 2013-07-03T07:08:36.250 回答
1

我找不到替换*//*空格的模式!,有两种模式用空格替换第 3 和第 4,其中第 3'/[\.\-\(\)]/'只针对以下字符.-(),而第 4 只针对您所说的空格。

另一点,第一个模式是在其他人看到它们之前删除它们,因为列表中的*and the/和不允许的字符,

所以在第一个模式之后,您将拥有以下字符串0()()()111 1-111111 ,它将继续执行其他模式。

于 2013-07-03T07:12:08.543 回答