php 正则

WBOY
Release: 2016-06-23 13:47:00
Original
1253 people have browsed it

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

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


回复讨论(解决方案)

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

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




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

在[] 里面可以不转义

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

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


为什么我测试这段这则如果中划线前面不加转义字符就会报错呢。
Warning: preg_replace(): Compilation failed: invalid range in character class at offset 5 in D:\xampp\htdocs\2.php on line 46
加上就可以了?
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template