Skip to content

Instantly share code, notes, and snippets.

@kjbrum
Last active May 8, 2018 19:18
Show Gist options
  • Save kjbrum/4c4a27cf2087b0e415d5 to your computer and use it in GitHub Desktop.
Save kjbrum/4c4a27cf2087b0e415d5 to your computer and use it in GitHub Desktop.
Get all of the strings between 2 strings.
<?php
/**
* Get all of the strings 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 array An array of all the string that were found between $start and $end
*/
function get_all_strings_between( $string, $start, $end, $case_sensitive=false ) {
$results = array();
$ini = ( $case_sensitive ) ? strpos( $string, $start ) : stripos( $string, $start );
if( $ini == 0 ) return $results;
$parts = explode( $start, $string );
array_shift( $parts );
foreach( $parts as $part ) {
$len = ( $case_sensitive ) ? strpos( $part, $end ) : stripos( $part, $end );
$results[] = substr( $part, 0, $len );
}
return $results;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment