正则表达式的应用 匹配、查找、分割、替换
来源:互联网 发布: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
- 正则表达式的应用 匹配、查找、分割、替换
- python 正则表达式 字符串的 匹配 替换 分割 查找
- Python 正则表达式 字符串的匹配、替换、分割、查找
- Editplus查找替换的正则表达式应用
- editplus查找替换的正则表达式应用
- editplus查找替换的正则表达式应用
- editplus查找替换的正则表达式应用
- editplus查找替换的正则表达式应用
- JAVA正则表达式匹配,替换,查找,切割的总结
- JAVA中正则表达式匹配,替换,查找,切割的方法
- 正则表达式匹配,替换,查找,切割的方法
- 正则表达式如何实现不替换查找的匹配文本?
- 正则表达式,模式匹配(查找替换)
- JAVA正则表达式匹配,替换,查找,切割
- JAVA正则表达式匹配,替换,查找,切割
- JAVA正则表达式匹配,替换,查找,切割
- JAVA正则表达式匹配,替换,查找,切割
- Java正则表达式匹配,替换,查找,切割
- android View
- 京东首页之页面顶部、Logo&搜索框
- go测试用例该如何写
- C#获取本机局域网ip和公网ip
- CSS基础入门1
- 正则表达式的应用 匹配、查找、分割、替换
- go与证书crl实践
- go中的slice坑
- 一行一步一花新:python-1
- 设计模式之禅
- 二级域名解析使用
- openssl-genras命令简单入门
- openssl的x509命令简单入门
- BZOJ2631: tree