正则去除括号

来源:互联网 发布:土地整理预算软件 编辑:程序博客网 时间:2024/03/29 10:00
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$str="你好<我>(爱)[北京]{天安门}";
echof1($str);//返回你好
echof2($str);//返回我
echof3($str);//返回爱
echof4($str);//返回北京
echof5($str);//返回天安门
functionf1($str)
{
$result=array();
preg_match_all("/^(.*)(?:<)/i",$str,$result);
return$result[1][0];
}
 
functionf2($str)
{
$result=array();
preg_match_all("/(?:<)(.*)(?:>)/i",$str,$result);
return$result[1][0];
}
functionf3($str)
{
$result=array();
preg_match_all("/(?:\()(.*)(?:\))/i",$str,$result);
return$result[1][0];
}
functionf4($str)
{
$result=array();
preg_match_all("/(?:\[)(.*)(?:\])/i",$str,$result);
return$result[1][0];
}
functionf5($str)
{
$result=array();
preg_match_all("/(?:\{)(.*)(?:\})/i",$str,$result);
return$result[1][0];
}

 

PS: (?:字符) 表示不捕获这个字符。貌似PHP不支持将字符换成括号。 


0 0
原创粉丝点击