How to remove numeric characters from string in php?

青灯夜游
Release: 2023-03-04 22:16:01
Original
2807 people have browsed it

在php中,可以使用preg_replace()函数配合正则表达式(/\\d+/)来去掉字符串中的数字字符;语法“preg_replace("/\\d+/",'', 指定字符串);”。preg_replace执行一个正则表达式的搜索和替换。

How to remove numeric characters from string in php?

推荐:《PHP视频教程

php去掉字符串中的数字字符

可以使用preg_replace()函数来删除字符串中的数字字符。此函数执行正则表达式搜索和替换。函数preg_replace()搜索由pattern(要搜索的模式)指定的字符串,如果找到则用替换替换模式。

方:1:正则表达式(/\\d+/)匹配使用数字字符,并用''(空字符串)替换它们。

preg_replace("/\\d+/",'', 指定字符串);
Copy after login

示例

<?php
// 包含数字字符的字符串
$str="php.cn2020"; 

// preg_replace函数删除数字字符
$str = preg_replace( &#39;/\\d+/&#39;, &#39;&#39;, $str);  
//打印字符串
echo($str); 
?>
Copy after login

输出:

php.cn
Copy after login
Copy after login

方法2:方法2:正则表达式'/ [0-9] / '匹配所有非字母数字字符,并用''(空字符串)替换它们。

$str = preg_replace( &#39;/[0-9]/&#39;, &#39;&#39;, $str);
Copy after login

在正则表达式中:

0-9:用于匹配所有数字。

示例:

Copy after login

输出:

php.cn
Copy after login
Copy after login

更多编程相关知识,可访问:编程入门!!

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

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