时间:2023-09-12加入收藏
$str = '666-888';
$res = substr($str, strripos($str, "-") + 1);
echo $res; // 输出 888,截取-之后的内容
$res = substr($str, 0, strrpos($str, "-"));
echo $res; // 输出 666,截取-之前的内容
正则:$str = '666-888';
echo getSubstr($str,'666','888');
function getSubstr($str, $leftStr, $rightStr){
$left = strpos($str, $leftStr);
$right = strpos($str, $rightStr,$left);
if($left < 0) return '';
if($right>0){
return substr($str, $left + strlen($leftStr), $right-$left-strlen($leftStr));
}else{
return substr($str, $left + strlen($leftStr));
} }
$str = '666-888';
preg_match("/666(.*?)888/is",$str,$res);
echo $res[1]; // 输出 - ,截取666到888之间的内容