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

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -