note 96455 added to function.preg-replace

From: Date: Sun, 28 Feb 2010 06:43:31 +0000
Subject: note 96455 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-166442@lists.php.net to get a copy of this message
Reading arguments against variables in CSS got me to thinking. Process your CSS files in a fashion similar to the following. This particular routine is certainly not the most efficient way, but is what I came up with on the spur of the moment. The prefix token is entirely arbitrary - everything between the leading colon and terminating semicolon is the target. In this way, default values can be put in place, and the constant identifiers simply left as comments, should the stylesheet be used without processing; this would also inhibit your editor from emitting errors about your odd syntax. The declaration pattern at the top assumes something like this: /*@css_const [ bgc_a=#ccccee, fc_a=#000099, bgc_b=#5555cc, bgc_c=#eeeeff, bgc_d=#599fee ] */ ...within the target CSS file. Usage like so: .Element { font-size:10pt; color:#000/*fc_a*/; background-color:#fff/*bgc_a*/; } And then... <?php $dec_pat = '/^\/\*\@css_const\s+\[(.*)\]\s+\*\//Ums'; preg_match_all($dec_pat,$css,$m); $lhs = array(); $rhs = array(); foreach($m[1] as &$p) { $p = explode(",",$p); foreach($p as &$q) { list($k,$v) = explode("=",trim($q)); $lhs[] = '/(\w+\:).*\/\*' . $k . '\*\/;$/Um'; $rhs[] = '\1' . $v . ';'; } } $css = preg_replace($lhs,$rhs,$css); // spit it out or return it; whatever ?> ...resulting, of course, in: .Element { font-size:10pt; color:#000099; background-color:#ccccee; } Again, efficiency was not the immediate goal, so please don't slay me... ---- Server IP: 64.71.164.2 Probable Submitter: 98.215.77.173 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/96455 Del: integrated -- https://master.php.net/note/delete/96455/integrated Del: useless -- https://master.php.net/note/delete/96455/useless Del: bad code -- https://master.php.net/note/delete/96455/bad+code Del: spam -- https://master.php.net/note/delete/96455/spam Del: non-english -- https://master.php.net/note/delete/96455/non-english Del: in docs -- https://master.php.net/note/delete/96455/in+docs Del: other reasons-- https://master.php.net/note/delete/96455 Reject -- https://master.php.net/note/reject/96455 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#166442) next »