Last active
August 29, 2015 14:04
-
-
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:
This file contains 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 | |
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