Home  >  Article  >  Backend Development  >  php 正则

php 正则

WBOY
WBOYOriginal
2016-06-23 13:47:001232browse

把所有非 .(点) -(负) 数字的都替换成空

如:-234.fgfg23   替换成-234.23
      234.中3/3       替换成234.33


回复讨论(解决方案)

echo preg_replace('/[^.-\d]+/', '', '-234.fgfg23');

echo preg_replace('/[^.-\d]+/', '', '-234.fgfg23');




/[^.-\d]+/  为何.不需要转义   \. 

在[] 里面可以不转义

\d 中的 \ 将 d 转义成数字
如果转义 \ 的话 \\d 就成 \ 字符后跟 d 字符了

echo preg_replace('/[^.-\d]+/', '', '-234.fgfg23');


为什么我测试这段这则如果中划线前面不加转义字符就会报错呢。
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 5 in D:\xampp\htdocs\2.php on line 46
加上就可以了?
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:thinkphp模板传值Next article:IIS7+php5.5+fastcgi