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

前端技术 2023/09/04 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];
}

本文地址:https://www.stayed.cn/item/11677

转载请注明出处。

本站部分内容来源于网络,如侵犯到您的权益,请 联系我

我的博客

人生若只如初见,何事秋风悲画扇。