Solarized
HOME > WEBプログラム覚書 > PEAR::HTML_CSSを使ってみる
PEAR::HTML_CSSを使ってみる
CSSを処理したり生成したりするためのPEAR::HTML_CSSを使ってみる。使う機会はあまりなさそうですが。
CSSファイルを変更
style.css
内容の取得変更するCSS
PHP
- <?php
- @CHARSET "utf-8";
-
- h1 {
- font-size: 160%;
- }
-
- h2 {
- font-size: 130%;
- }
-
- h3 {
- font-size: 130%;
- }
-
-
- width: 300px;
- height: 200px;
- padding: 1em;
- background:
- }
- ?>
テストコード
PHP
- <?php
- require_once 'HTML/CSS.php';
-
- define('DIR_CSS', '/css/');
- $css_filename = DIR_CSS . 'style.css';
-
-
-
- $css = new HTML_CSS();
-
-
- $result = $css->parseFile($css_filename);
-
- if (is_null($result)) {
-
-
- $css->setStyle('#html_css', 'width', '500px');
-
-
- $css->setStyle('#html_css_add', 'width', '200px');
-
-
-
-
- $hxs = array_keys($css->grepStyle('/^h[1-6]$/'));
- $group_selector = implode(', ', $hxs);
-
- if (!array_key_exists($group_selector, $css->toArray())) {
-
-
- $group_id = $css->createGroup(implode(',', $hxs));
-
-
- $css->setGroupStyle($group_id, 'padding', '0px');
- $css->setGroupStyle($group_id, 'font-weight', 'bigger');
-
-
- echo '<h4>Validate結果</h4>';
- $message = array();
- var_dump('結果は? ', $css->validate(array($css->toString()), $message));
- var_dump('<pre>', $message, '</pre>');
- echo '<hr />';
-
-
- $css->setGroupStyle($group_id, 'font-weight', 'normal');
-
-
- echo '<h4>2回目:Validate結果</h4>';
- var_dump('結果は? ', $css->validate(array($css->toString()), $message));
-
-
- echo '<h4>書き込みデータ</h4>';
- echo '<pre>';
- echo $css->display();
- echo '</pre>';
-
- echo '<hr />';
-
-
- $css->toFile($css_filename);
-
-
- $css->setSingleLineOutput(true);
- $css->setLineEnd('');
- $css->toFile(DIR_CSS . 'compress.css');
- }
-
- } else {
- throw new Exception($result->getType() . '::' . $result->getMessage());
- }
- ?>
実行結果
Validate結果
string(13) "結果は? " bool(false) string(5) "
"
array(2) {
["errors"]=>
array(1) {
[0]=>
array(6) {
["errortype"]=>
string(11) "parse-error"
["context"]=>
string(12) " h1, h2, h3 "
["property"]=>
NULL
["uri"]=>
NULL
["line"]=>
string(1) "5"
["message"]=>
string(235) "
Value Error : font-weight (http://www.w3.org/TR/CSS21/fonts.html#propdef-font-weight)
bigger is not a font-weight value :
"
}
}
["warnings"]=>
array(0) {
}
}
string(6) "
"
2回目:Validate結果
string(13) "結果は? " bool(true)
書き込みデータ
@charset "utf-8";
h1, h2, h3 {
padding: 0px;
font-weight: normal;
}
h1 {
font-size: 160%;
}
h2 {
font-size: 130%;
}
h3 {
font-size: 130%;
}
#html_css {
width: 500px;
height: 200px;
padding: 1em;
background: #CCCCCC;
}
#html_css_add {
width: 200px;
}
一行にまとめたcompress.cssは下記のようになります。
PHP
- <?php
- @charset "utf-8";h1, h2, h3 { padding: 0px; font-weight: normal;}h1 { font-size: 160%;}h2 { font-size: 130%;}h3 { font-size: 130%;}
- ?>
※Windows環境でグループとか使ってtoFile()で上書きすると結果がおかしくなる場合があるっぽい。
CMSとかジェネレータ作る時とかつかえそうです。あとは他のサイトの調査に使えます。
W3CのCSS確認
PHP
- <?php
- require_once 'HTML/CSS.php';
- $css = new HTML_CSS();
- $result = $css->parseFile(file_get_contents('http://www.w3.org/2008/site/css/minimum'));
- $message = array();
- var_dump('結果は? ', $css->validate(array($css->toString()), $message));
- ?>
実行結果
おっ!?vaildだw
| 投稿日 |
2009年11月22日 16:01 |
| カテゴリ |
PHP |
| タグ |
PEAR | サンプルコード | テストコード |
| トラックバック URL |
http://www.kantenna.com/cgi-bin/mt504/mt-tb.cgi/1195 |
2009年12月21日 01:58
PEAR::Calenderを利用して特定の日付からn日間表示続きを読む
2011年9月14日 13:17
「WEBプログラム覚書::[PHP]google Analytics からアクセスの多いページの情報を取得」に引き続きGoogle Analytics A...続きを読む
2011年9月14日 13:17
google AnalyticsのAPIを利用してアクセスの多いページの情報を取得します。続きを読む
2011年9月14日 13:18
ファイルの情報をオブジェクト指向っぽく取得したりするためのクラスSplFileInfo 続きを読む