Parse Php File Variables From Python Script
I need to get some data from PHP(Wordpress) config files from my Python script. How I can parse config data? For example, how I can get $wp_version value? Config example: /** * Th
Solution 1:
You know that a simple variable in PHP is like $foo = 'bar';, let's create a regex that does not take in account something like $_GET or $foo['bar']:
- Start with
$, note that we need to escape it:\$ - The first character after
$can't be a number and has to be a letter or underscore:\$[a-z] - Then there may be a letter or digits or underscore after it:
\$[a-z]\w* - Let's put the parenthesis:
\$([a-z]\w*) - Now then there should be the "equal sign", but to make it more compatible, let's make the spaces optional:
\$([a-z]\w*)\s*=\s* - After this there should be a value and it ends with a
;:\$([a-z]\w*)\s*=\s*(.*?);$ - We will use the
mmodifier which make^$match start and end of line respectively. - You can then use a trimming function to get ride of the single and double quotes.
Note 1: This regex will fail at nested variables $fail = 'en_EN'; $fail2 = 'en_EN';Note 2: Don't forget to use the i modifier to make it case insensitive.
Solution 2:
I've written a little python script to get pull database login information from wordpress's wp-config.php file for doing automatic site backups.
Here is the relevant part of my code (GitHub's syntax highlighting has trouble with Python's triple quoted strings):
#!/usr/bin/env python3import re
define_pattern = re.compile(r"""\bdefine\(\s*('|")(.*)\1\s*,\s*('|")(.*)\3\)\s*;""")
assign_pattern = re.compile(r"""(^|;)\s*\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\s*=\s*('|")(.*)\3\s*;""")
php_vars = {}
for line inopen("wp-config.php"):
for match in define_pattern.finditer(line):
php_vars[match.group(2)]=match.group(4)
for match in assign_pattern.finditer(line):
php_vars[match.group(2)]=match.group(4)
Post a Comment for "Parse Php File Variables From Python Script"