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

php使用正则表达式提取字符串中尖括号、小括号、中括号、大括号中的字符串

创建时间:2015-12-20 投稿人: 浏览次数:2238
</pre><p>PHP使用正则表达式提取字符串中尖括号<>、小括号()、中括号[]、大括号{}中的字符示例,需要的朋友可以参考下</p><p></p><p><pre name="code" class="php">$str="你好<我>(爱)[北京]{天安门}"; 
echo f1($str); //返回你好 
echo f2($str); //返回我 
echo f3($str); //返回爱 
echo f4($str); //返回北京 
echo f5($str); //返回天安门 
function f1($str) 
{ 
$result = array(); 
preg_match_all("/^(.*)(?:<)/i",$str, $result); 
return $result[1][0]; 
} 

function f2($str) 
{ 
$result = array(); 
preg_match_all("/(?:<)(.*)(?:>)/i",$str, $result); 
return $result[1][0]; 
} 
function f3($str) 
{ 
$result = array(); 
preg_match_all("/(?:()(.*)(?:))/i",$str, $result); 
return $result[1][0]; 
} 
function f4($str) 
{ 
$result = array(); 
preg_match_all("/(?:[)(.*)(?:])/i",$str, $result); 
return $result[1][0]; 
} 
function f5($str) 
{ 
$result = array(); 
preg_match_all("/(?:{)(.*)(?:})/i",$str, $result); 
return $result[1][0]; 
} 


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