user852689
user852689

Reputation: 751

Extract all words double wrapped in curly braces

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

Answers (3)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57690

Using pattern /\{\{(\w+)\}\}/ you can extract all the words

preg_match_all("/\{\{(\w+)\}\}/", $str, $matches);
print_r($matches[1]);

http://ideone.com/pyB4D

Upvotes: 4

Bono
Bono

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

Andreas Wong
Andreas Wong

Reputation: 60594

<?php
$vars = '{{this}} {{is}}{{a}} {{test}}';

$matches = array();
preg_match_all('/{{([^}]+)}}/', $vars, $matches);
var_dump($matches[1]);

Like that?

Upvotes: 1

Related Questions