Reputation: 751
I have a string that is formatted like the following:
... {{word1}} {{word2}} .... etc
I need to extract all the words, located inside the '{{' and '}}' tags.
What is the most efficient way to retrieve the words from the string in PHP?
Upvotes: 0
Views: 66
Reputation: 57690
Using pattern /\{\{(\w+)\}\}/
you can extract all the words
preg_match_all("/\{\{(\w+)\}\}/", $str, $matches);
print_r($matches[1]);
Upvotes: 4
Reputation: 4869
Alternative to look into is the PHP explode function
Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.
Upvotes: 1
Reputation: 60594
<?php
$vars = '{{this}} {{is}}{{a}} {{test}}';
$matches = array();
preg_match_all('/{{([^}]+)}}/', $vars, $matches);
var_dump($matches[1]);
Like that?
Upvotes: 1