Skip to content

Instantly share code, notes, and snippets.

@tonyspiro
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonyspiro/de4195fa81e68ed80f77 to your computer and use it in GitHub Desktop.
Save tonyspiro/de4195fa81e68ed80f77 to your computer and use it in GitHub Desktop.
Here’s a cool function that I use quite often. It looks for two different strings, a beginning and end string, from the original string and returns the middle remainder. Super useful if you do a lot of get_file_contents() and API coding. Check it out:
<?php
function getBetween($content,$start,$end){
$r = explode($start, $content);
if (isset($r[1])){
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
?>
Example:
<?php
$content = "Try to find the guy in the middle with this function!";
$start = "Try to find ";
$end = " with this function!";
$output = getBetween($content,$start,$end);
echo $output;
?>
This will return "the guy in the middle".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment