Created
August 17, 2016 01:22
-
-
Save Jany-M/8aeb24a7ec7da0014cfa821b6b323e8b to your computer and use it in GitHub Desktop.
[PHP] Parse a string between two strings
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 | |
// PHP – parse a string between two strings | |
// Credits: http://www.justin-cook.com/wp/2006/03/31/php-parse-a-string-between-two-strings/ | |
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); | |
} | |
$fullstring = "this is my [tag]dog[/tag]"; | |
$parsed = get_string_between($fullstring, "[tag]", "[/tag]"); | |
echo $parsed; // (result = dog) | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment