Created
November 22, 2022 10:15
-
-
Save mehdichaouch/0da7692684bacf7570fc8dbe76d3572e to your computer and use it in GitHub Desktop.
PHP - Get a substring 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 | |
/** | |
* @param string $string | |
* @param string $start | |
* @param string $end | |
* | |
* @return bool|string | |
* @see https://stackoverflow.com/questions/5696412/how-to-get-a-substring-between-two-strings-in-php | |
* | |
*/ | |
public function getStringBetween(string $string, string $start, string $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