牛骨文教育服务平台(让学习变的简单)
博文笔记

正则去除括号

创建时间:2015-12-11 投稿人: 浏览次数:2587
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不支持将字符换成括号。 


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。