note 100204 added to function.preg-replace

From: Date: Thu, 30 Sep 2010 21:38:53 +0000
Subject: note 100204 added to function.preg-replace
Groups: php.notes 
Request: Send a blank email to php-notes+get-172858@lists.php.net to get a copy of this message
I needed a function to convert short color notations (e.g. #3a0) to the long version (i.e. #33aa00). As I didn't find one, I immersed into unfamiliar territory: PCRE. But after some trials and errors, I managed to write a function that doubles all hex characters and drops any other. <?php function colorlong ($color) { return preg_replace ("/(?(?=[^0-9a-f])[^.]|(.))/i", '$1$1', $color); } //--- some examples --- echo colorlong ('d08'); //--- dd0088 --- echo colorlong ('bad beer'); //--- bbaaddbbeeee --- echo colorlong ('nothing'); //--- yep, nothing ;-) --- echo colorlong ('E6f'); //--- EE66ff: case is not changed --- ?> ---- Server IP: 85.214.154.209 Probable Submitter: 79.208.172.55 ---- Manual Page -- http://www.php.net/manual/en/function.preg-replace.php Edit -- https://master.php.net/note/edit/100204 Del: integrated -- https://master.php.net/note/delete/100204/integrated Del: useless -- https://master.php.net/note/delete/100204/useless Del: bad code -- https://master.php.net/note/delete/100204/bad+code Del: spam -- https://master.php.net/note/delete/100204/spam Del: non-english -- https://master.php.net/note/delete/100204/non-english Del: in docs -- https://master.php.net/note/delete/100204/in+docs Del: other reasons-- https://master.php.net/note/delete/100204 Reject -- https://master.php.net/note/reject/100204 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#172858) next »