-1

I have a string that contains multiple templated variables like this:

$str = "Hello ${first_name} ${last_name}";

How can I do to extract these variables in an array like this :

$array = ['first_name', 'last_name'];
5
  • you can make string blade compatible and use string rendering method reference Commented Jan 4, 2023 at 13:45
  • Actually I can't use string blade I have to use this format :/ Commented Jan 4, 2023 at 13:47
  • then you can write a custom string parser function. maybe this can help you. Commented Jan 4, 2023 at 13:51
  • I'm trying to find a regex to detect this pattern and use preg_split() after Commented Jan 4, 2023 at 13:54
  • The variables will just be interpolated into the string by PHP, throwing an undefined variable notice if they don't exist, and a deprecation notice either way. What does this have to do with Laravel? Commented Feb 9, 2024 at 17:08

3 Answers 3

1

Use single quotes instead of double quotes when describing the string.

$str = 'Hello ${first_name} ${last_name}';
preg_match_all('/{(.*?)}/s', $str, $output_array);
dd($output_array[1]);
Sign up to request clarification or add additional context in comments.

2 Comments

It's possible to get directly the good array without specify [1] ?
@John 0 is the full match. 1 is the first capture group. Each index is a capture group, after the full match which is always 0. see also: php.net/manual/en/function.preg-match-all.php if you don't want to specify index you can use end()
1

Use explode function example given below:

$str = "Hello ${first_name} ${last_name}";
    
$str_to_array = explode("$",$str);
$array = array();
foreach($str_to_array as $data){
    if (str_contains($data, '{')) {
        $array[] = str_replace("}","",str_replace("{","",$data));
    }
}

print_r($array);

1 Comment

Your solution is working. I'm searching for a solution with regex pattern for less code. But your solution is nice. You just have to specify that str_contains is for php 8
1

You can use a simple regular expression and use preg_match_all to find all the ocurrences, like this:

<?php
    $pattern = '|\${.*?}|';
    $subject = 'Hello ${first_name} ${last_name}';
    $matches = '';
    preg_match_all($pattern, $subject, $matches);
    print_r($matches);
?>

Result:

Array
(
    [0] => Array
        (
            [0] => ${first_name}
            [1] => ${last_name}
        )

)

EDIT: To get just the names of the variables, a small modification of the code will make it possible:

<?php
    $pattern = '|\${(.*?)}|'; // We added parenthesis making '.*?' a sub-pattern now.
    $subject = 'Hello ${first_name} ${last_name}';
    $matches = '';
    preg_match_all($pattern, $subject, $matches);
    print_r($matches);
?>

Result:

Array
(
    [0] => Array
        (
            [0] => ${first_name}
            [1] => ${last_name}
        )
    [1] => Array
        (
            [0] => first_name
            [1] => last_name
        )
)

Now the first element of the result array contains the matched results for the full regex expression, and the second one contains the matched results for the sub-pattern.

1 Comment

It's a nice solution but how can I do to return only the variable name in the array ? I just need first_name and last_name

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.