Re: substrings

From: Date: Sun, 25 Apr 2004 20:10:35 +0000
Subject: Re: substrings
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-184583@lists.php.net to get a copy of this message
$color = '#aabbcc'; if (strlen($color) == 7) { echo ' r: ' . substr($color, 1, 2); echo ' g: ' . substr($color, 3, 2); echo ' b: ' . substr($color, 5, 2); } elseif (strlen($color) == 4) { echo ' r: ' . str_repeat(substr($color, 1, 1), 2); echo ' g: ' . str_repeat(substr($color, 2, 1), 2); echo ' b: ' . str_repeat(substr($color, 3, 1), 2); } Sunday, April 25, 2004, 10:00:30 PM, thus was written: > Hi, all -- > I must be having a brain fart as well as going blind because I can't find > in the manual how to do this. > Given a string like #AABBCC or #112233 how do I get the > 2nd-3rd > 4th-5th > 6th-7th > position substrings so I can break out the red, green, and blue values? > All of the regular expressions seem to hinge on matching a separator, and > the closest thing seems to be a > preg_match(".(..)(..)(..)",$color,$colors) ; > $r = $colors[1] ; > $g = $colors[2] ; > $b = $colors[3] ; > but it just seems like I should be able to return a list like > ($r,$g,$b) = fn(operands,$color) ; > or so. > TIA & HAND > :-D

« previous php.general (#184583) next »