How to remove numbers from string in php

藏色散人
Release: 2023-03-08 08:52:01
Original
2899 people have browsed it

php去掉字符串中的数字的方法:首先创建一个PHP示例文件;然后通过正则表达式“preg_replace("/\\d+/",'', $res);”匹配字符串中的数字并删除即可。

How to remove numbers from string in php

本文操作环境:Windows7系统、PHP7.1、Dell G3电脑。

php去掉字符串中的数字

这个比较简单,但是也有些需要注意的地方,先贴代码

$class=preg_replace("\\d+",'', $res);
Copy after login

需要使用preg_replace函数,但是只是这么写的话,会报错

Warning: preg_replace(): Delimiter must not be alphanumeric or backslash
Copy after login

翻译过来就是定界符不能是字母数字或反斜线。

想了一下,在正则表达式首尾加了一对/

$class=preg_replace("/\\d+/",'', $res);
Copy after login

这样就成功了

【推荐:PHP视频教程

The above is the detailed content of How to remove numbers from string in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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