Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active November 11, 2015 20:35
Show Gist options
  • Save kjbrum/c3bed4f106ae89bb2fa2 to your computer and use it in GitHub Desktop.
Save kjbrum/c3bed4f106ae89bb2fa2 to your computer and use it in GitHub Desktop.
<?php
/**
* Get a string between 2 strings.
*
* @param string $string The string to search
* @param string $start The string to start with
* @param string $end The string to end with
* @param boolean $case_sensitive Whether to make the search case sensitive or not
* @return string The string that was found between the start and end
*/
function get_string_between( $string, $start, $end, $case_sensitive=true ) {
$string = " " . $string;
$ini = ( $case_sensitive ) ? strpos( $string, $start ) : stripos( $string, $start );
if( $ini == 0 ) return false;
$ini += strlen( $start );
$len = ( $case_sensitive ) ? strpos( $string, $end, $ini ) - $ini : stripos( $string, $end, $ini ) - $ini;
return substr( $string, $ini, $len );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment