Home >Backend Development >PHP Problem >How to use regular expressions to replace in php
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=[\\\'| \\\"](.*?(?:[\.gif|\.jpg]))[\\\'|\\\"].*?[\/]?>/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)
# Split the time according to underscores, ${3} is the matching The third atom in $patterns ((\d{1,2})), ${4} matches the fourth atom in $patterns ((\d{1,2})), and so on. , For example, the method of compilation and replacement in the familiar smarty template engine,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!