Last active
November 11, 2015 20:35
-
-
Save kjbrum/c3bed4f106ae89bb2fa2 to your computer and use it in GitHub Desktop.
Get a string between 2 strings. Original: http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-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 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