Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

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>
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #substring #strings
ADD COMMENT
Topic
Name
3+7 =