tags:

views:

66

answers:

3

morning. I am wanting to take all segments of php code out of a file located on my local server. Problem is i dont seem to be getting anywhere, no php errors just browser errors.

$file_contents = "<xmp>".file_get_contents("../www.cms.actwebdesigns.co.uk2/pageIncludes/instalation/selectMainPages.php")."</xmp>";

if(preg_match_all("#<\?php((?!\?>).)*#is", $file_contents, $matches))
{
    foreach($matches[0] as $phpCode)
    {
     $code = "<xmp>".$phpCode."\n?></xmp>";
    }
}
echo "dsds";
?>

could someone please point me in the right direction?

+1  A: 

You might want to tokenize the PHP script using the Tokenizer extension:

http://php.net/manual/en/book.tokenizer.php

The extensions is built into PHP since PHP v4.3.0.

$tokens = token_get_all(file_get_contents($file));

http://www.php.net/manual/en/function.token-get-all.php

thephpdeveloper
Not sure how to use this. Puts all code into an array. For me to use it wouldn't i have to implode it or something then im back to square one?
Phil Jackson
you can loop through and remove all those which are not PHP code.
thephpdeveloper
A: 

Not sure how to use this. Puts all code into an array. For me to use it wouldn't i have to implode it or something then im back to square one?

Phil Jackson
This is either a comment on an answer or you should just edit your question.
random
+2  A: 

working with this:

$file_contents = token_get_all(file_get_contents("../www.cms.actwebdesigns.co.uk2/logged.php"));
$start=0;
$end=0;
$segmentArray = array();
foreach($file_contents as $key => $token)
{
    $tokenName = token_name($key);
    if($start==0 && $end==0 && $tokenName=="T_OPEN_TAG")
    {
     $start=1;
    }
    if(start==1 && $end==0 && $tokenName!="T_CLOSE_TAG")
    {
     $entryNo = count($segmentArray);
     $segmentArray[$entryNo][] = $token;
    }
    if($tokenName=="T_CLOSE_TAG")
    {
     $start=0; 
    }
}
Phil Jackson
that's awesome =) congrats!
thephpdeveloper
Running into a problem. Alot of the token names are returning UNKNOWN. any one know why?
Phil Jackson