Reputation: 287
I want to get the template words in a string. Template in the sense of words in {} inside a string. I am adding here a code to explain what I want exactly.
$string = "Hi {username}, Here is your {password}";
function get_template_variables($string)
{
....
....
return array();
}
$result = get_template_variables($string);
print_r($result);
array[
0 => username,
1 => password
]
I hope you understand what I need. I need the definition program to be used inside the get_template_variables($string)
method
Upvotes: 0
Views: 380
Reputation: 120694
function get_template_variables($string) {
preg_match_all('/{([^}]+)}/', $string, $matches);
return $matches[1];
}
Upvotes: 4
Reputation: 5303
preg_match_all('/{.*}/U' , $string , $a );
where $result will be
array(1) {
[0]=>
array(2) {
[0]=>
string(10) "{username}"
[1]=>
string(10) "{password}"
}
}
Upvotes: 0
Reputation: 455272
You can do:
function get_template_variables($string) {
if(preg_match_all('/\{(.*?)\}/',$string,$m)) {
return $m[1]
}
return array();
}
Upvotes: 2