Last active
November 21, 2015 12:58
-
-
Save TangChr/47517d0ced65ca1ee7b3 to your computer and use it in GitHub Desktop.
PHP: Read comment-based file-info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function get_file_info( $file ) { | |
$fp = fopen( $file, 'r' ); | |
$data = fread( $fp, 8192 ); // get first 8kb | |
fclose( $fp ); | |
// Capture all the header within first comment block | |
if( !preg_match( '!.*?/\*(.*?)\*/!ms', $data, $matches ) ) | |
return array(); | |
// Capture each line with "Something: some text" | |
unset( $data ); | |
$lines = preg_split( "[\n|\r]", $matches[1] ); | |
unset( $matches ); | |
$file_data = array(); | |
foreach( $lines as $line ) { | |
if( !preg_match( '!(.*?):\s+(.*)!', $line, $matches ) ) | |
continue; | |
list( $null, $field, $value ) = array_map( 'trim', $matches); | |
$file_data[ $field ] = $value; | |
} | |
return $file_data; | |
} | |
/* READ TEST FILES */ | |
$php = get_file_info('test.php'); | |
var_dump($php); | |
?> | |
<br /> | |
<br /> | |
<br /> | |
<?php | |
$css = get_file_info('test.css'); | |
var_dump($css); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Name: test.css | |
Language: CSS | |
Author: TangChr | |
Author URI: http://christiantang.dk | |
*/ | |
/* Just some useless stuff */ | |
* { | |
margin: 0; | |
padding: 0; | |
} | |
body { | |
background: goldenrod; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Name: test.php | |
Language: PHP | |
Author: TangChr | |
Author URI: http://christiantang.dk | |
*/ | |
// Just some useless stuff | |
function concat_strings($str0, $str1) { | |
return ($str0.$str1); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment