我喜欢这两种解决方案
function GetBetween($content,$start,$end)
{
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
function get_string_between($string, $start, $end){
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0) return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
我上面的两个解决方案也做了很少的基准测试,两者都给出了几乎相同的时间。你也可以测试一下。我给两个函数一个文件来读取有大约60000个字符(用Word的字数统计),这两个函数导致大约0.000999秒的查找。
$startTime = microtime(true);
GetBetween($str, '', '');
echo "Explodin Function took: ".(microtime(true) - $startTime) . " to finish
";
$startTime = microtime(true);
get_string_between($str, '', '');
echo "Subsring Function took: ".(microtime(true) - $startTime) . " to finish
";