PHP Classes

PHP CSS Parser and Optimizer: Modify HTML with CSS to optimize the C

Recommend this page to a friend!
  Info   View files Example   View files View files (10)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-08 (5 days ago) RSS 2.0 feedNot yet rated by the usersTotal: 8 This week: 8All time: 11,220 This week: 34Up
Version License PHP version Categories
styleparser 1.0MIT/X Consortium ...7HTML, PHP 5, Parsers
Description 

Author

This package can modify HTML with CSS to optimize the styles.

It can take a string with the HTML of a Web page and parses it to make certain modifications.

The package can read configuration files to determine how the modifications of the HTML page will be applied.

Currently, it can parse the CSS style definitions and rewrite the styles to compact the style definitions.

Picture of Sheldon Kennedy
Name: Sheldon Kennedy <contact>
Classes: 2 packages by
Country: South Africa South Africa

Example

<?php

include '../src/CSSParser.php';

$html = file_get_contents('sample.html');

$cssParser = new CSSParser();
$modifiedHtml = $cssParser->parse($html);

echo
$modifiedHtml;


Details

StyleParser

This PHP class reduces download overhead by excluding unused CSS classes.

Introduction

StyleParser analyses an HTML string and identifies the CSS styles being used. It then injects the corresponding classes from your own custom library directly back into the HTML <style> tag. If there is no <style> tag it will create one.

StyleParser includes an optional, responsive utility library that provides all the reusable classes you need to build a site using a simple and intuitive syntax. This utility library uses the data-util attribute, instead of the standard class attribute, in order to keep utilities separate from your own custom classes. You can change the attribute by editing config.ini.

Usage

  1. Instantiate StyleParser:
    $styleParser = new StyleParser();
    
  2. Parse HTML:
    $html = '<html>...</html>';
    $modifiedHtml = $styleParser->parse($html);
    
  3. Customisation: - Modify configuration files to tailor the styling behaviour.

Configuration Files

Please note that classes must be written using PHP .ini syntax inside the config files, and not standard CSS syntax.

  • `config.ini` for general settings.
  • `root.ini` for root styles whiach are applied globally.
  • `elements.ini` for standard CSS elements (e.g. `div`).
  • `custom.ini` for custom classes (e.g. `header-hero`).
  • `utilities.ini` for built-in utility attributes (e.g. `row(lc)s+`).
  • `resolutions.ini` for built-in utility media query resolutions.

Dependencies

  • PHP: Requires PHP 7.0 or later.
  • DOMDocument: Uses the `DOMDocument` class for HTML parsing.

Error Handling

The class throws a RuntimeException if parsing any configuration file fails. It is essential to handle this exception appropriately in your application.

License

This project is licensed under the MIT License.

Acknowledgments

Feel free to contribute, report issues, or suggest improvements!


  Files folder image Files  
File Role Description
Files folder imagedemo (2 files)
Files folder imagesrc (1 file, 1 directory)
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  demo  
File Role Description
  Accessible without login Plain text file RUN_ME.php Example Example script
  Accessible without login HTML file sample.html Data Sample HTML file

  Files folder image Files  /  src  
File Role Description
Files folder imageconfigs (6 files)
  Plain text file CSSParser.php Class Class source

  Files folder image Files  /  src  /  configs  
File Role Description
  Accessible without login Plain text file config.ini Data Auxiliary data
  Accessible without login Plain text file custom.ini Data Auxiliary data
  Accessible without login Plain text file elements.ini Data Auxiliary data
  Accessible without login Plain text file resolutions.ini Data Auxiliary data
  Accessible without login Plain text file root.ini Data Auxiliary data
  Accessible without login Plain text file utilities.ini Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:8
This week:8
All time:11,220
This week:34Up