I want to get an array of the following types of strings:
data {string1} {string2} {string3} data
I want to get an array whose values are string1/string2/strin3. How can I do this?
I want to get an array of the following types of strings:
data {string1} {string2} {string3} data
I want to get an array whose values are string1/string2/strin3. How can I do this?
One way of doing it would be exploding it:
$string = "data string1 string2 string3 data";
$parts = explode(" ", $string);
echo $parts[1]; // string1
echo $parts[2]; // string2
echo $parts[3]; // string3
But if you could provide some more information about your data it would be easier to come up with a better approch.
You could try:
$data = '';
$matches = array();
if (preg_match_all('#\\{([^}]*)\\}#', $string, $matches, PREG_PATTERN_ORDER)) {
$data = implode('/', $matches[1]);
}
echo $data;
$str = "data {string1} {string2} {string3} data";
preg_match_all('/{\w*}/',$str, $matches);
echo $matches[0][0]; // {string1}
echo $matches[0][1]; // {string2}
echo $matches[0][2]; // {string3}
one-liner w/o capturing parentheses, the string resides in $stuff:
$arr = preg_match_all('/(?<={)[^}]+(?=})/', $stuff, $m) ? $m[0] : Array();
result:
foreach($arr as $a) echo "$a\n";
string1
string2
string3
Regards
rbo