/    Sign up×
Community /Pin to ProfileBookmark

Read PHP files as text, extract variable values

I am in a need to go through php files in a folder, i.e list all php files in that folder, read contents of each file, check whether few predefined variable names are present in code, if present read their values.

For example say a particular php file contains following variables.

$value1 = ‘value of one’;
$value2 = ‘value of two’;

Then my script should be capable of reading content of that file, extract values of $value1 and $value2.

Is there any simpler way to do that.

Really a great help if you can suggest a solution and that will be highly appreciated.

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@jedaisoulFeb 10.2016 — Simpler than what? You have not given your code...
Copy linkTweet thisAlerts:
@GUIRauthorFeb 10.2016 — Well I think regex helps but I dont understand how to build the expression to match a variable name in code. So far I dont have code with me, If I clarify further i m looking a regex or similar solution to match/search pattern similar to a variable name, varibale name is known in advanced.

assumed search variable is found, then for extraction of variable value 'assigned in code' , the possible solution I see is string manipulation, i meant replacing content of line containing variable other than the variable value with empty strings. Apart form that is there a way to get the value.

Suppose in code,

[code=php]$value1 = 'value of one';
$value2 = 'value of two';[/code]


so regex or other solution is search for string "$value1" & "$value2"

then it should be possible read out 'value of one' & 'value of two' as values of respective variables detected in code string.

Thank you.
Copy linkTweet thisAlerts:
@NogDogFeb 10.2016 — Sounds like it could get pretty ugly. What if you need to handle this assignment:
[code=php]
$var1 = $this->getValue(
$foo,
$this->bar->getAnotherValue($foo),
SOME_CONSTANT
);
[/code]

?
Copy linkTweet thisAlerts:
@jedaisoulFeb 11.2016 — Rather than using REGEX, I would treat the source file as simple text and scan it line by line for the text "$var1" (or whatever). If found, I'd output the whole line. This will not necessarily tell you the value set.
Copy linkTweet thisAlerts:
@GUIRauthorFeb 14.2016 — Thanks , I am goiing to do it,
×

Success!

Help @GUIR spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...