note 96455 deleted from function.preg-replace by cmb

From: Date: Mon, 21 Nov 2016 19:37:33 +0000
Subject: note 96455 deleted from function.preg-replace by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207304@lists.php.net to get a copy of this message
Note Submitter: roscoe-p-coltrane ---- 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...

« previous php.notes (#207304) next »