kelly
kelly

Reputation: 1

Parsing a list of values of the form `{value}` with regular expressions

I have one string like the following (the number of the {} is unknown):

{test}{test1}{test2}{test3}{test4}

Now I like to get the content in {} out and put them into array. How can I do this? I tried:

preg_match( "/(\{{\S}+\}*/)*")

But the result is wrong. Thanks so much for anyone's help.

Upvotes: 0

Views: 131

Answers (3)

binaryLV
binaryLV

Reputation: 9122

try preg_match_all('/\{(.+)\}/U', $text)

Upvotes: 1

erisco
erisco

Reputation: 14329

<?php
$string = '{test}{test1}{test2}{test3}{test4}';
$parts = explode('}{', substr($string, 1, -1));

This solution avoids using regular expressions which are often slower than simple string functions. Also, many programmers want to avoid regular expressions whenever practical due to preference.

Upvotes: 5

Matteo Riva
Matteo Riva

Reputation: 25060

preg_match_all('/{(.*?)}/', $string, $match);
print_r($match[1]);

Upvotes: 5

Related Questions