正则表达式的应用 匹配、查找、分割、替换

来源:互联网 发布:java初级工资待遇 编辑:程序博客网 时间:2024/05/16 17:20
正则表达式的应用匹配、查找、分割、替换<?php $haystack = "this is a test";//找到第一个包括i的后面的子字符串$needle = "i";echo strstr($haystack,$needle);//is is s test----------------------------------------------------<?php $haystack = "this is a test";$needle = "i";//找到最后一次i出现的位置echo strrpos($haystack,$needle);----------------------------------------------------<?php /* * 1、有如下地址:    $url = "http://www.baidu.com/index.php"    $url = "../image/a.jpg"   写一个自定义函数,获取不同url地址下的文件名称。 */$url = "http://www.baidu.com/index.php";$url = "../image/a.jpg";echo getFilename($url);function getFilename($url){    //取得"/"在url最后一次出现的位置    $pos = strrpos($url,"/");    $filename = substr($url,$pos+1);    return $filename;}----------------------------------------------------<?php $subject = "this is a test";$pattern = "/(t)/";//preg_match_all是二维数组var_dump(preg_match_all($pattern,$subject,$match));echo "<hr/>";var_dump($match);echo "<hr/>";echo "<hr/>";//preg_match是一维数组var_dump(preg_match($pattern,$subject,$match));echo "<hr/>";var_dump($match);----------------------------------------------------<?php /* * 2、给一组电话号码   $input = array("18500698888","123456789","18634771800");   找出合法的电话号码。 *///正则匹配$input = array("18500698888","123456789",               "18634771800");$pattern = "/^1[34578]\d{9}$/";var_dump(preg_grep($pattern,$input));----------------------------------------------------<?php /* * 2、给一组邮箱地址   $emails = array("tom@qq.com","jack.john@qq.com","163.com");   找出合法的邮箱地址。 *///正则匹配$emails = array("tom@qq.com",        "jack.john@qq.com",         "163.com");$pattern='/\w+([\.\-]\W+)?@\w+\.(com|cn|net|org)/';var_dump(preg_grep($pattern,$emails));----------------------------------------------------<?php/* * 、字符串的分割   有如下字符串:   $str = "this is a test"   用字符串中的字母 s 对字符串进行分割 *///字符串的分割$str = "this is a test";//explode 把字符串切割成数组$arr=explode('s',$str);echo '<pre>';print_r($arr);echo '</pre>';//implode 和join 把数组连接成字符串$str=join('s',$arr);echo $str;----------------------------------------------------<?php//正则分割/* * 4、有如下的字符串    $str = "th:i,s i.s@ a te%st"    应用字符串的分割处理函数。在字符串中按照特殊符号进行分割。 */$str = "th:i,s i.s@ a te%st";$pattern = "/[\:\,\.\@\%]/";$arr=preg_split($pattern,$str);echo '<pre>';print_r($arr);echo '</pre>';----------------------------------------------------<?phpheader("Content-Type:text/html;charset=utf-8");/* * 5、应用字符串替换函数,对下面的字符串进行替换:   今天的晚饭是:"米饭、面条、炒饼"   替换成喜欢吃的饭:   今天的晚饭是:"" *///字符串的替换$str = "今天的晚饭是:米饭、面条、炒饼";$search=array('米饭','面条','炒饼');$replace=array('火锅','自助餐','麻辣面');//数组的话,需要一一对应//$replace='**';可以直接使用字符串替换,就不用考虑这些问题echo str_replace($search,$replace,$str);//返回的结果是替换了的新$str字符串//今天的晚饭是:火锅、自助餐、麻辣面----------------------------------------------------<?php header("Content-Type:text/html;charset=utf-8");//正则替换$subject = "this is a test";$pattern = "/is/";$replace = "";echo preg_replace($pattern,$replace,$subject);----------------------------------------------------<?php header("Content-Type:text/html;charset=utf-8");//正则替换$subject = "我是一个含有<span style='color:red;'>HTML</span>标签        的<b>字符</b>串";//$pattern = "/<\/?[\w\s\=\'\"\:\;]+>/";$pattern = "/<[^<>]+>/";$replace = "";echo $subject;echo "<hr/>";echo preg_replace($pattern,$replace,$subject);----------------------------------------------------<?phpheader("Content-Type:text/html;charset=utf-8");$subject = "abcdhttps://www.baidu.comabchttp://www.sohu.comabcdhttp://music.yahoo.comabcdftp://www.163.com";//$pattern = "/(https?|ftps?):\/\/(\w+)\.(\w+)\.(com|cn|net|org)/";$pattern = "/(https?|ftps?):\/\/(\w+)\.(\w+)\.(com|cn|net|org)/";//这里面有一个小技巧,使用\\1 \\2 \\3 \\4 反向引用 分别取出数组里面的东西,既然是存放在数组里面的,那么全局可用$replace = '<a href="\1://\2.\3.\4">\\0</a>';//查看一下抓取到的$match数组preg_match_all($pattern,$subject,$match);echo '<pre>';print_r($match);echo '</pre>';echo preg_replace($pattern,$replace,$subject);/*Array(    [0] => Array    (        [0] => https://www.baidu.com            [1] => http://www.sohu.com            [2] => http://music.yahoo.com            [3] => ftp://www.163.com        )    [1] => Array(    [0] => https            [1] => http            [2] => http            [3] => ftp        )    [2] => Array(    [0] => www            [1] => www            [2] => music            [3] => www        )    [3] => Array(    [0] => baidu            [1] => sohu            [2] => yahoo            [3] => 163        )    [4] => Array(    [0] => com            [1] => com            [2] => com            [3] => com        ))*/
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电视显示没有usb设备怎么办 华为平板电脑触摸屏没反应怎么办 平板电脑液晶显示屏坏了怎么办 6p的home键失灵怎么办 读书郎屏幕坏了怎么办 苹果平板激活锁忘了怎么办 平板电脑充电插口坏了怎么办? 电视频幕花了怎么办 苹果笔记本电脑硬盘坏了怎么办 mac屏幕压坏了怎么办 新电脑连不上网怎么办 新买电脑后悔了怎么办 装显卡后玩游戏黑屏怎么办 顺丰寄主机坏了怎么办 征信账户忘记了怎么办 疑似qq和疑似应用宝怎么办 荣大3608速印机卸版故障怎么办 配的近视眼镜看不清电脑屏幕怎么办 cad命令反应很慢怎么办 淘宝美工面试让作图怎么办 笔记本玩cf闪退怎么办 台式电脑玩lol卡怎么办 配置充足玩lol卡怎么办 电脑买贵了应该怎么办 微信红包收不了钱怎么办 mac版excel卡死怎么办 锐捷网卡为空怎么办 淘宝上买东西卖家不发货怎么办 快递员没给单号怎么办 淘宝物流把货弄丢了怎么办 刚买的卡没激活怎么办 腾讯王卡40g用完怎么办 手机欠费不知道电话号码怎么办 手机欠费了不用了怎么办 闲鱼恶意差评怎么办 淘宝买家账户体检中心违规怎么办 淘宝卖家账户体检中心违规怎么办 淘宝好评被删除评价怎么办 淘宝没收到货确认收货了怎么办 美团评论被删怎么办 拼多多恶意差评怎么办