Skip to content

Instantly share code, notes, and snippets.

@TangChr
Last active November 21, 2015 12:58
Show Gist options
  • Save TangChr/47517d0ced65ca1ee7b3 to your computer and use it in GitHub Desktop.
Save TangChr/47517d0ced65ca1ee7b3 to your computer and use it in GitHub Desktop.
PHP: Read comment-based file-info
<?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);
?>
/*
Name: test.css
Language: CSS
Author: TangChr
Author URI: http://christiantang.dk
*/
/* Just some useless stuff */
* {
margin: 0;
padding: 0;
}
body {
background: goldenrod;
}
<?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