views:

53

answers:

1

hi, 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

+2  A: 

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, '?>') 
);
Pekka
thank you very much. you help me a lot. i try to create PHPdoc mini in my spare time on learning PHP. i try to accomplish this before the end of the year ;D
justjoe
@justjoe good luck with it! A brilliant project idea in my opinion.
Pekka