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'];
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'];
3
Answers
Use explode function example given below:
You can use a simple regular expression and use
preg_match_all
to find all the ocurrences, like this:Result:
Use single quotes instead of double quotes when describing the string.