php正则表达式实现替换的方法:首先创建一个PHP示例文件;然后定义一个字符串;最后通过正则表达式“preg_replace("/[0-9]/","",$str);”去掉字符串中所有数字即可。
本教程操作环境:Windows7系统、PHP5.6版,该方法适用于所有品牌电脑。
推荐:《PHP视频教程》
PHP正则替换preg_replace函数的使用
<?php $str="as2223adfsf0s4df0sdfsdf"; echo preg_replace("/0/","",$str);//去掉0字符,此时相当于 replace的功能, preg_replace("/0/","A",$str); 这样就是将0变成A的意思了 echo preg_replace("/[0-9]/","",$str);//去掉所有数字 echo preg_replace("/[a-z]/","",$str); //这样是去掉所有小写字母 echo preg_replace("/[A-Z]/","",$str); //这样是去掉所有大写字母 echo preg_replace("/[a-z,A-Z]/","",$str); //这样是去掉所有字母 $str="as2223adfsAAf0s4df0s中国人dD中南海DDfsdf"; echo preg_replace("/[a-z,A-Z,0-9]/","",$str); //去掉所有字母和数字 ?> 经过以上的例子,相信大家知道,[ ] 和里面的,有什么作用了。也可以看到,匹配的字符串必须加 / /(看例子的第一个参数) <?php $str="acsdcs<55555555>sc<6666>sdcd"; echo preg_replace("/<.*>/","",$str); //这个是表示去除以<开头,以>结尾的那部份,输出结果是:acsdcssdcd 注意:上面的 .* 是表示任何字符,也就是说不管<>包住的是什么都去掉其中. 表示任意字符, * 表示任意个数 现在我们来改动一下,如果不想是任何个数呢? $str="acsdcs<55555555>sc<6666>sdcd"; echo preg_replace("/<.{4}>/","",$str); //此时输出:acsdcs<55555555>scsdcd因为{4}指定了条件:<>内为4个字符的才满足条件,所以<55555555>不符合条件,没有被替换。 注意:这时我们又学到了一个知识点{数字} 表示指定前面的个数,*就表示是任意个(0--无限个) 表示重复次数的除了 *, {指定次数} 表示,还有很多表达形式: <?php $str="acsdcs<55555555>sc<6666>sd<>cd"; echo preg_replace("/<[0-9]*>/","",$str); //输出acsdcscd echo "<hr>"; echo preg_replace("/<[0-9]+>/","",$str); //输入acsdcsscsd<>cd ?> 上面的例子只要是为了 表达 * 与+的区别 ,* 表示重复0数或n 次,而+ 表示1次以上,即一例中<[0-9]+ >表示<>里面至少要有一个数字才符合条件。 相信这时大家知道,为什么上例中用* 和用+输出的结果不同了吧 再来: <?php $str="acsdcs<55555555>sc<6666>sd<>cd"; echo preg_replace("/<[0-9]?>/","",$str); //输出acsdcs<55555555>sc<6666>sdcd ?> 看[0-9]? 这里的?表示要是0次或1 次,超过1次又不符合条件了。 总结一下,上面我们学会了 * + ? 和大括号{}表示重复次数的方法。 ---------------------------------------------------------------------------------------------------- $s=preg_replace("/(.*?[月票|求|更].*?)/i","",$s); preg_match_all('/href=\"([0-9]+)\.shtm">(.+?)<\/a>/i',$s,$arr_dstorycate); print_r($arr_dstorycate); ---------------------------------------------------------------------------------------------------- preg_match_all("/<img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/020/6873a8a82997d63a0f86be7a6c63b830-0.png?x-oss-process=image/resize,p_40" class="lazy" .*?src=[\\\'| \\\"](.*?(?:[\.gif|\.jpg]))[\\\'|\\\"].*?[\/]? alt="How to use regular expressions to replace in php" >/i",$content,$arr_dstorycate); print_r($arr_dstorycate);
补充:
.$pattern是数组,$replace也是数组,则中对应的 元素进行替换
php
preg_replace有五个参数,有三个是必须参数
Preg_replace(mixed $pattern, mixed $replacement, mixed $subject [,int $limit = -1 [,int $count ]]);
$pattern 可以是字符串,字符串数组,或者preg
$replace 是用于替换的字符串或字符串数组
$subject 目标字符串或者目标字符串数组
$limit 每个模式在每个$subject上最大的替换次数,默认是-1(无限次)
$count 返回在目标字符串所替换的次数
在repalce与pattern中有几点要注意的
1.$pattern是数组,$replace也是字符串,那么所有模式都使用这个字符串来匹配;
2.$pattern是数组,$replace也是数组,则中对应的 元素进行替换
3.如果数组$pattern大于$replace,则多出来的$pattern用空的字符串进行替换,
$replace少于$patterns,则匹配到的值全部用空字符串来替换
4. For the ${1}, ${2}, ${3}, that appear in the above example, this is called a backward reference , and the numbers in represent atoms. The number of digits and can be written in two ways , one is \\n, and one is ${n}, is recommended Use the second way of writing . The atoms , in the pattern that the target string matches will change the format of the target string , or The matching atoms are output as they are (here are two examples)
,The fourth parameter means the maximum number of substitutions for each pattern on each subject
If limit is n, it only replaces n times with regular expressions
6. The use of the fifth parameter $count, $count means, The number of times the subject target string has been replaced, take the fifth point as an example
The example running result in the above code is as follows:
Additional:
What is the relationship between preg_replace and str_replace?
We can understand that str_replace is a subset of preg_repalce. It is just the first in str_replace Each parameter is written as a string, and preg_replace is written as a regular expression
The above is the detailed content of How to use regular expressions to replace in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor