Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php get string between two strings

$str = 'before-str-after';
if (preg_match('/before-(.*?)-after/', $str, $match) == 1) {
    echo $match[1];
}
Comment

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>
Comment

PREVIOUS NEXT
Code Example
Php :: force delete soft delete laravel 
Php :: convert query result to array php 
Php :: php expire session 
Php :: php check undefined offset 
Php :: php controller 
Php :: php sort array by specific key 
Php :: convert image to base64 in laravel 
Php :: set session after login with laravel 
Php :: how to use where relationship laravel 
Php :: laravel create seeder 
Php :: php for each schleife 
Php :: laravel get all users except role spatie 
Php :: php const 
Php :: php clone 
Php :: the posts pagination 
Php :: php convert object to array nested 
Php :: laravel group route controller 
Php :: laravel use session values in view 
Php :: mac address php 
Php :: larvel make http request 
Php :: substract 2 dates php 
Php :: snap store phpstrom 
Php :: php get current dir mac 
Php :: for in php 
Php :: scribe laravel 
Php :: check if index exists in array php 
Php :: how to retrieve data from database using select option in laravel 
Php :: Clear php cache 
Php :: angular post phph 
Php :: define constant in php 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =