I have the following problem:
I have a Text with the e.g. the following Format:
min: 34.0 max: 79.0383 lifetime: 17% code:iweo7373333
It’s not a fixed text Type, means min can also be -7.94884444 or so. How can i extract the parts in e.g. an array like
$result['min'] = 34.0;
$result['max'] = 79.0383
and so on...
I did it at the moment with replacing spaces, then replace “min:” with nothing, “max:”, “lifetime:”, … with “,” and then an explode… The main Problem is that sometimes other variables are between min, max, …. so the positions do not hold the correct values.
Also – i think – it’s not a really good coding style or? Is this possible with regex or preg_replace?
Thanks,
Sascha
There is no need to be authoritative about it. It depends on your purposes. I would personally opt for JSON in this case. XML can be an overkill most of the times.
The only advantage I see in keeping that format you proposed is that it has no need for complex syntax using
{}()[];,(and it seems you don’t need nesting).This regex will match all the
parameter:valuecombinations from your string, being very tolerant with use of whitespace on values:(?<=^| )[A-Za-z-_]{0,}:[.,\$\-\+\s%\w]{0,}(?<=\s|\Z|^)So in PHP:
Output: