Forum Moderators: coopster

Message Too Old, No Replies

CSS Comment Parser just like in Wordpress

         

paperso

7:09 pm on Oct 18, 2011 (gmt 0)

10+ Year Member



Hello All!

What I want to achieve is to be able to parse css comments and print them in php.

This is the style.css:
/*
Plugin Name: PLug name
Plugin URI: http://www.domain.com
Description: This is css
Version: 1.0
Author: My Name
Author URI: http://www.domain.com/my-name
*/


Now, I want to print in php the plugin name, plugin uri and so on.

Is there a way to acheive this?

paperso

3:48 pm on Oct 19, 2011 (gmt 0)

10+ Year Member



I guess no one here knows how this done.

rocknbil

4:45 pm on Oct 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Patience is a virtue. Or no one understood the question. For example, you're saying you want **just** the info between the comments? All comments in the file, or just the top chunk? What if the format varies?

There's a couple ways, depends on what you're familiar with. What I'd do is slurp the whole file into a scalar, capture everything between the comment delimiters into an array using a preg_match, then print out the array.

paperso

8:28 pm on Oct 22, 2011 (gmt 0)

10+ Year Member



function get_plugins_header()
{
$from_folder = 'plugin';
$extension = '.php';
if ( $handle = @opendir ( $from_folder ) )
{
while ( $file = readdir ( $handle ) )
{
if (is_file ( $from_folder . $file ) )
{
if (strpos ( $from_folder . $file, $extension ) )
{
$fp = fopen ( $from_folder . $file, 'r' );
// Pull only the first 8kiB of the file in.
$plugin_data = fread ( $fp, 8192 );
fclose ( $fp );
preg_match ( '|Plugin Name:(.*)$|mi', $plugin_data, $name );
preg_match ( '|Plugin URI:(.*)$|mi', $plugin_data, $uri );
preg_match ( '|Version:(.*)|i', $plugin_data, $version );
preg_match ( '|Description:(.*)$|mi', $plugin_data, $description );
preg_match ( '|Author:(.*)$|mi', $plugin_data, $author_name );
preg_match ( '|Author URI:(.*)$|mi', $plugin_data, $author_uri );
foreach ( array ('name', 'uri', 'version', 'description', 'author_name', 'author_uri' ) as $field )
{
if (! empty ( ${$field} )) ${$field} = trim ( ${$field} [1] ); else ${$field} = '';
}
$plugin_data = array ('filename' => $file, 'Name' => $name, 'Title' => $name, 'PluginURI' => $uri, 'Description' => $description, 'Author' => $author_name, 'AuthorURI' => $author_uri, 'Version' => $version );
$plugin_headers [] = $plugin_data;
}
}
elseif ( ( is_dir( $from_folder . $file ) ) && ( $file != '.' ) && ( $file != '..' ) )
{
get_plugins_header ( $from_folder . $file . '/' );
}
}
closedir ( $handle );
}
return $plugin_headers;
}

$get = get_plugins_header();
foreach ( $get as $plugin_header ) {

echo '<strong>URI</strong>: ' . $plugin_header ['PluginURI'] . '<br />';
echo '<strong>Title</strong>: ' . $plugin_header ['Title'] . '<br />';
echo '<strong>Name</strong>: ' . $plugin_header ['Name'] . '<br />';
echo '<strong>Version</strong>: ' . $plugin_header ['Version'] . '<br />';
echo '<strong>Description</strong>: ' . $plugin_header ['Description'] . '<br />';
echo '<strong>Author URL</strong>: ' . $plugin_header ['AuthorURI'] . '<br />';
echo '<strong>Author</strong>: ' . $plugin_header ['Author'];

}


style.css
/*
Plugin Name: Twentyone
Plugin URI: http://domain.com
Description: This is style
Version: 1.0
Author: Kiljoy97
Author URI: http://www.domain.com/
*/


@ rocknbil I have this code above. If you change the file extension to php the function works just fine. Now if you change it to .css it doesn't work and it keeps on printing. Try it yourself because it's kinda hard to explain it here.