Convert python code to php -
i found solution of task on python. not know python @ all. code:
import re import json = open('test.txt').read() = re.sub('"[ \t]*"', '":"', a) = re.sub('"\s+"', '","', a) = re.sub('"\s+{', '":{', a) = re.sub('}\s+"', '},"', a) = '{%s}' % b = json.loads(a)
tried convert php analogy:
$a = file_get_contents('test.txt'); $a = preg_replace('"[ \t]*"', '":"', $a); $a = preg_replace('"\s+"', '","', $a); $a = preg_replace('"\s+{', '":{', $a); $a = preg_replace('}\s+"', '},"', $a); $a = '{'.$a.'}'; $b = json_decode($a);
but seems expressions wrong. can it?
in php think regular expressions need inside 2 x /
e.g.
$a = preg_replace('/"[ \t]*"/', '":"', $a);
edit: andré mentioned, delimiter not need forward slashes, think double quotes you're using meant used part of regular expression rather delimiters.
Comments
Post a Comment