PEAR logo

HTML_CSS : The Definitive Guide

Parsing data sources usage

Parse a string that contains CSS information
Parse multiple data sources at once

Parse a string that contains CSS information

In this example, here are our goal :

  • load CSS definitions from a simple PHP string
  1. *{
  2. margin: 0px; padding: 0px;
  3. }
  4.  
  5. body{
  6. font-family: Lucida Grande, Tahoma, Verdana, Arial, sans-serif;
  7. text-align:center;
  8. background-color:#fff;
  9. }

Example 11.3. Simple string parsing

  1. <?php
  2. require_once 'HTML/CSS.php';
  3.  
  4. $styles = '
  5. *{
  6. margin: 0px; padding: 0px;
  7. }
  8.  
  9. body{
  10. font-family: Lucida Grande, Tahoma, Verdana, Arial, sans-serif;
  11. text-align:center;
  12. background-color:#fff;
  13. }
  14. ';
  15.  
  16. $css = new HTML_CSS();
  17. $css->parseString($styles);
  18. ?>

Parse multiple data sources at once

In this example, here are our goals :

  • load CSS definitions from a simple PHP string
  • load CSS definitions from multiple CSS files

Here is content of print.css file :

  1. *{
  2. margin: 4px; padding: 0px;
  3. }
  4.  
  5. body{
  6. font-family: Tahoma, Verdana, Helvetica, Arial, sans-serif;
  7. text-align:center;
  8. background-color:#fff;
  9. }

Here is content of default.css file :

  1. *{
  2. margin: 0px; padding: 0px;
  3. }
  4.  
  5. body{
  6. font-family: Lucida Grande, Tahoma, Verdana, Arial, sans-serif;
  7. text-align:center;
  8. background-color:#fff;
  9. }

Example 11.4. Multiple data sources parsing

  1. <?php
  2. require_once 'HTML/CSS.php';
  3.  
  4. $styles = array(
  5.     "body { font-size: 1em; }",
  6.     "print.css",
  7.     "default.css"
  8. );
  9.  
  10. $css = new HTML_CSS();
  11. $css->parseData($styles);
  12. $css->display();
  13. ?>

And here are the result we should expected :

  1. body {
  2.   font-size: 1em;
  3.   font-family: Lucida Grande, Tahoma, Verdana, Arial, sans-serif;
  4.   text-align: center;
  5.   background-color: #fff;
  6. }
  7.  
  8. * {
  9.   margin: 0px;
  10.   padding: 0px;
  11. }
HTML_CSS : The Definitive Guide v 1.5.0 : January 15, 2008