根据给出的前后内容截取字符,个人封装了一个方法,还挺好用的。
<?php
function cutstr($haystack, $before_needle, $after_needle) {
$start = is_int($before_needle) ? $before_needle: (strpos($haystack, $before_needle) + strlen($before_needle));
$length = is_int($after_needle) ? $after_needle: (strpos($haystack, $after_needle, $start) - $start);
return substr($haystack, $start, $length);
}
示例
<?php
$contents = 'abcdef123456!@#$';
$strAB = cutstr($contents, 'd', '$');
$strA = cutstr($contents, 'a', 4);
$strB = cutstr($contents, 1, '$');
print $strAB
print $strA
print $strB
// 打印结果
// ef123456!@#
// bcde
// bcdef123456!@#