0

Hi,
I am using preg_match_all first time and i have problem with getting values from string.
Example of the string:

$str = '...
{
	"trailStart":"1980",
	"key":{"dim0":"Switzerland"}
}
... ';

Here is my example. I have to get out year and country (1980, Swizerland).
Can anybody write pattaren for me because i can't get it right.
Thanks

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by ShawnCplus
0

It would be better to expand it to two lines or a custom function like the following:

<?
function getarray($i) {
    $i=str_replace(array('(',')'),'',$i);
    return explode(',',$i);
    }
$input='(1980,Swizerland)';
$array=getarray($input);
echo '<xmp>';
print_r($array);
echo '</xmp>';
?>

I'm not sure if that is exactly what you want but matches what you have described.

0

Thanks for replay, but this is not what i was looking for. I will try to explain it otherwise.

I get string like this:

<?
$string =  '"yZoomedDataMax":82060000,"time":"1980","sizeOption":"_UNISIZE","xLambda":1,"xZoomedDataMin":20273,"yZoomedDataMin":1353355,"stateVersion":3,"xAxisOption":"3","playDuration":15,"iconKeySettings":[{[B]"trailStart":"1980"[/B],"key":{[B]"dim0":"Netherlands"[/B]}},{[B]"trailStart":"1980"[/B],"key":{[B]"dim0":"Switzerland"[/B]}},{[B]"trailStart":"1980"[/B],"key":{[B]"dim0":"Austria"[/B]}}],"yAxisOption":"4","yZoomedIn":false,"xZoomedIn":false,"orderedByY":false,"colorOption":"2"}';
?>

First i was trying to use explode, but i thought it would be easier to use preg_match to get certain values from it.
Values from this string that i need are 'trailStart":"year"' and '"dim0":"country"'.

So what i need is pattern for this function

<?
preg_match_all($pattern, $str, $matches);
?>

and then this function returns results in array "$matches".

0

Although I'm not sure how you want the surrounding data split, the data mentioned in post #1 would be matched like the following:

<?
$string = '"yZoomedDataMax":82060000,"time":"1980","sizeOption":"_UNISIZE","xLambda":1,"xZoomedDataMin":20273,"yZoomedDataMin":1353355,"stateVersion":3,"xAxisOption":"3","playDuration":15,"iconKeySettings":[{"trailStart":"1980","key":{"dim0":"Netherlands"}},{"trailStart":"1980","key":{"dim0":"Switzerland"}},{"trailStart":"1980","key":{"dim0":"Austria"}}],"yAxisOption":"4","yZoomedIn":false,"xZoomedIn":false,"orderedByY":false,"colorOption":"2"}';
//echo $string;
preg_match_all('/\{"([^"]+)":"([^"]+)","([^"]+)":\{"([^"]+)":"([^"]+)"\}\}/',$string,$array);
echo '<xmp>';
print_r($array);
echo '</xmp>';
?>
0

Although I'm not sure how you want the surrounding data split, the data mentioned in post #1 would be matched like the following:

<?
$string = '"yZoomedDataMax":82060000,"time":"1980","sizeOption":"_UNISIZE","xLambda":1,"xZoomedDataMin":20273,"yZoomedDataMin":1353355,"stateVersion":3,"xAxisOption":"3","playDuration":15,"iconKeySettings":[{"trailStart":"1980","key":{"dim0":"Netherlands"}},{"trailStart":"1980","key":{"dim0":"Switzerland"}},{"trailStart":"1980","key":{"dim0":"Austria"}}],"yAxisOption":"4","yZoomedIn":false,"xZoomedIn":false,"orderedByY":false,"colorOption":"2"}';
//echo $string;
preg_match_all('/\{"([^"]+)":"([^"]+)","([^"]+)":\{"([^"]+)":"([^"]+)"\}\}/',$string,$array);
echo '<xmp>';
print_r($array);
echo '</xmp>';
?>

This is marked as solved but it wasn't solved correctly. This just just JSON data, use json_decode

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.