正则表达式的应用
匹配、查找、分割、替换
<?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>';
----------------------------------------------------
<?php
header("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);
----------------------------------------------------
<?php
header("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
)
)*/
正则表达式的应用 匹配、查找、分割、替换
最新推荐文章于 2021-03-09 17:51:59 发布