PHP截取两组多个指定字符中间的字符

2024-11-02 10:20:15
推荐回答(2个)
回答(1):

function strbetweenAB($srcstr,$str1,$str2)
{
$i=strrpos($srcstr,$str1);
$j=strrpos($srcstr,$str2);
$b=substr($srcstr,$i+strlen($str1),$j-$i-strlen($str1));
return $b;
}
这个函数你看行不行?

回答(2):

$a = "123456789";
$b = "34";
$c = "8";
sj($a, $b, $c, $d);
//将提示:567
tw($d);

//从头部开始截取
sj($a, null, $c, $d);
tw($d);

//截取到尾部
sj($a, $b, null, $d);
tw($d);

说明:
用于截取数据部分字符

function sj($a,$m1,$m2,& $re){
if($m1!=null && $m2!=null){
$t1 = mb_strpos($a, $m1)+strlen($m1);
$htx = mb_substr($a,$t1);
$t2 = strlen($htx)+strlen($m1)+mb_strpos($htx, $m2);
$re = mb_substr($a,$t1,$t2-$t1);
return "type:[iApp-sj]";
}
else if($m1==null && $m2!=null){
$t2 = mb_strpos($a, $m2);
$re = mb_substr($a,0,$t2);
return "type:[iApp-sj]";
}
else if($m1!=null && $m2==null){
$t1 = mb_strpos($a, $m1)+strlen($m1);
$t2 = 0;
$re = mb_substr($a,$t1);
return "type:[iApp-sj]";
}
return -1;
}