How to get a substring between two strings in PHP? code example
Example: How to get a substring between two strings in PHP?
<!DOCTYPE html>
<html>
<body>
<?php
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 = '.layout {
color: {{ base_color }}
}
li {
color: {{ sub_color }}
}
.text {
color: {{ txt_color }}
}
.btn {
color: {{ btn_color }}
}
.more_text{
color:{{more_color}}
}';
$arr = [];
while(1){
$parsed = get_string_between($fullstring, '{{', '}}');
if(!$parsed)
break;
array_push($arr,$parsed);
$strposition = strpos($fullstring,"}}");
$nextString = substr($fullstring, $strposition+1, strlen($fullstring));
$fullstring = $nextString;
echo "<br>";
}
print_r($arr);
?>
</body>
</html>