I'm trying to find simple key-value-pairs in strings, given as JSON-objects, while using preg_replace_callback().
Unfortunately, the values given can be of type string, number, boolean, null, array - and worst of all - objects. My own attempts solving this problem resulted in either an incomplete selection or over-selecting multiple JSON occurances as one.
Here the things i tried:
String:
text text {"key":{"key":"value"}} text
Regex:
\{"(.+?)"\:(.+?)\}
Match:
{"key":"value"
Above: This ignores the inner }-bracket
String:
text text {"key":{"key":"value"}} text
Regex:
\{"(.+?)"\:(.+)\}
Match:
{"key":"value"}
Above: This would (theoretically) work, but when having multiple JSON occurances, i get:
{"key":"value"}} {"key":{"key":"value"}
Next attempt:
String:
text text {"key":{"key":"value"}} {"key":{"key":"value"}} text
Regex:
\{"(.+?)"\:(?:(\{(?:.+?)\})|(?:")?(.+?)(?:")?)\}
Match:
{"key":"value"}
Above: Again, that would theoreticcally work. But when taking, for example, the following string:
text text {"key":{"key":{"key":"value"}}} text
The result is...
{"key":{"key":"value"}
Missing one bracket