php get string between two strings

39

$str = 'before-str-after';
if (preg_match('/before-(.*?)-after/', $str, $match) == 1) {
    echo $match[1];
}
<!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>

Comments

Submit
0 Comments