函数preg_replace()与str_replace()

如截图,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); //去掉所有字母和数字
    ?>
与str_replace用法同,只是str_ireplace不区分大小写
$str=str_ireplace("a"," ","abcabcAbca");

 

其实,preg_replace与str_replace的用法大致差不多,preg_replace倾向于正则替换,str_replace倾向于普通替换,怎么使用,就具体情况而言。

 

posted @ 2015-12-23 13:21  侠岚之弋痕夕  阅读(279)  评论(0编辑  收藏  举报
Where is the starting point, we don't have a choice, but the destination where we can pursue!