Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP function to return string between two strings
/**
* Get the portion of a string between two given strings.
*
* @param string $subject
* @param string $start
* @param string $end
* @param bool $insensitive
* @param bool $innerOnly
* @param bool $inverse
*
* @return string
*/
public static function between($string, $start, $end, $insensitive = false, $innerOnly = true, $inverse = false)
{
if ($insensitive) {
$string = strtolower($string);
$start = strtolower($string);
$end = strtolower($end);
}
if (($startPosition = strpos($string, $start)) === false || ($endPosition = strpos($string, $end)) === false) {
return false;
}
if ($innerOnly) {
$startPosition += strlen($start);
$endPosition -= strlen($end);
}
$between = substr($string, $startPosition, ($endPosition + strlen($end)) - $startPosition);
if ($inverse) {
return str_replace($between, '', $string);
}
return $between;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment