Skip to content

Instantly share code, notes, and snippets.

@justinfay
Last active February 3, 2017 11:03
Show Gist options
  • Save justinfay/52e947af939ca31cc0af095a0a0ec1d3 to your computer and use it in GitHub Desktop.
Save justinfay/52e947af939ca31cc0af095a0a0ec1d3 to your computer and use it in GitHub Desktop.
"""
Quick POC for parsing a PHP array from python.
"""
from pyparsing import *
"""
array
( members )
()
members
key => value
members , key => value
key
string | int
value
string | number | array | null | true | false
"""
NULL = Keyword('NULL').setParseAction(replaceWith(None))
TRUE = Keyword('TRUE').setParseAction(replaceWith(True))
FALSE = Keyword('FALSE').setParseAction(replaceWith(False))
LPAREN = Suppress("array (")
RPAREN = Suppress(")")
ARROW = Suppress("=>")
COMMA = Suppress(",")
array = Forward()
value = Forward()
key = Forward()
string = sglQuotedString().setParseAction(removeQuotes)
number = pyparsing_common.number()
key << (number | string)
value << (string | number | Group(array) | NULL | FALSE | TRUE)
members = delimitedList(Group(key + ARROW + value))
array << Dict(LPAREN + Optional(members) + Optional(COMMA).suppress() + RPAREN)
def from_php(chars):
"""
Parse a PHP array from a string created
with `var_export`.
"""
result = array.parseString(chars)
# For some reason `asDict` coerces int keys to strings.
return result.asDict(), result.asList()
if __name__ == "__main__":
import pprint
test_data = """
array (
'astring' => '74525',
'ind_code' =>
array (
0 => '74525',
'fpp' => 'IND_420'),
'sector_code' => 423.423,
1 => FALSE,
)
"""
pprint.pprint(from_php(test_data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment