Skip to content

Instantly share code, notes, and snippets.

@hedii
Last active March 4, 2019 10:19
Show Gist options
  • Save hedii/320c5aa45e11ddd765e7 to your computer and use it in GitHub Desktop.
Save hedii/320c5aa45e11ddd765e7 to your computer and use it in GitHub Desktop.
Php get_string_between function
<?php
/**
* get_string_between function.
*
* Given a string ($string) and two delimiters ($start and $end), this function
* returns the substring between $start and $end. $start and $end are also
* removed.
*
* @link http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/
*
* @access public
* @param string $string
* @param string $start
* @param string $end
* @return string the string between $start and $end
*/
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) {
return '';
}
$ini += strlen($start);
$len = strpos($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