Last active
May 8, 2018 19:18
-
-
Save kjbrum/4c4a27cf2087b0e415d5 to your computer and use it in GitHub Desktop.
Get all of the strings between 2 strings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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