Can any body help to split php source code
-
19-09-2019 - |
Question
can any body help me on separating this example of data that i need to parse and seperate text just like PHPDoc. It is PHP source code.
The example string :
function one_to_tree() {
//bla bla bla
return FALSE;
}
function two_to_tree() {
//bla bla bla
return FALSE;
}
function three_to_tree() {
if ($sample){ //bla bla bla }
return FALSE;
}
can anybody help me how to seperate above string based on "function" word and create and array. Thank you
Solution
I think you are looking for token_get_all().
This function uses the PHP parser to split PHP source code into tokens. I think it's safe to say it's the most reliable method of parsing PHP code - if it's usable for whatever you are planning to do.
An example from the manual:
$tokens = token_get_all('<?php echo; ?>');
Results in
array(
array(T_OPEN_TAG, '<?php'),
array(T_ECHO, 'echo'),
';',
array(T_CLOSE_TAG, '?>')
);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow