In PHP's strpos
style this will return false
if the start mark sm
or the end mark em
are not found.
This result (false
) is different from an empty string that is what you get if there is nothing between the start and end marks.
function between( $str, $sm, $em )
{
$s = strpos( $str, $sm );
if( $s === false ) return false;
$s += strlen( $sm );
$e = strpos( $str, $em, $s );
if( $e === false ) return false;
return substr( $str, $s, $e - $s );
}
The function will return only the first match.
It's obvious but worth mentioning that the function will first look for sm
and then for em
.
This implies you may not get the desired result/behaviour if em
has to be searched first and then the string have to be parsed backward in search of sm
.