So, I will talk about this simple json string example, covering most of my real cases:
"time":1430702635,\"id\":\"45.33\",\"state\":2,"stamp":14.30702635,
And I'm trying to replace preg with numbers from a string to quote them, except for numbers whose index is already quoated, for example, in my line - '\ state \': 2 So far my regular expression
preg_replace('/(?!(\\\"))(\:)([0-9\.]+)(\,)/', '$2"$3"$4',$string);
The resulting string that I get to get in this case has the value "\ state \" without quotes, omitted by the regular expression, because it contains "in front: a digit,
"time":"1430702635",\"id\":\"45.33\",\"state\":2,"stamp":"14.30702635",
Why is the number \ state \ also replaced?
Tried https://regex101.com/r/xI1zI4/1 as well.
New edit:
So from what I tried,
(?!\\")
does not work!
, , - , .
, , NOT, .
$string2 = preg_replace('/(\w":)([0-9\.]+)(,)/', '$1"$2"$3',$string);
.