2011-11-26 35 views
0

我有下面的代码:PHP的preg_replace不工作像预期

<?php 
    $test = "xxxx..AAA?!"; 
    echo $test."\n"; 
    $test = preg_replace("[^a-zA-Z0-9-]", "", $test); 
    echo $test."\n"; 
?> 

我想删除不属于字母,数字字符全部或减号

什么是我的错?

回答

4

分隔符缺少

$test = preg_replace('/[^a-zA-Z0-9-]/', '', $test); 
echo $test . "\n"; 

此外,我建议使用的PHP_EOL代替"\n"为换行符。

+0

啊谢谢!!现在工作:) – soupdiver

+0

:D太棒了!不用说 –

+0

注意:如果使用:'$ test = preg_replace('/ [^ a-zA-Z0-9 - ] + /','',$ test);'它会更快,因为它需要做更少的操作 – noob