Forum Moderators: coopster

Message Too Old, No Replies

Whats the simplest way to filter all the occurances of a html colour?

Filtering out HTML colors (#000000) from a string

         

raingerpete

11:32 pm on May 6, 2003 (gmt 0)

10+ Year Member



I have a string $RetrievedInfo and I would like to "pull" out all the occurances of a HTML color e.g. #000000.

What would the best and simplest way to do this?

I would also then want to remove any duplicates?

I.e. I want a variable, most probably an array that contains all the colours used in a html page, from which I have already retrieved the source for.

Pete

grahamstewart

11:43 pm on May 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well your unlikely to get all the colours used on a page from the html, because most pages these days use CSS to specify colours.

But to answer your question, a regular expression is probably your best bet.

Assuming your using PHP, it could look something like..


$matches = array();

preg_match_all( '/#[0-9A-F]{6}/im', $RetrievedInfo, $matches );

print_r( $matches );

That should match all 6 figure hex digits in RetrievedInfo that start with a hash symbol.

grahamstewart

11:45 pm on May 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh and you can remove duplicates from an array using array_unique()

gethan

11:49 pm on May 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi ... try

preg_match_all("¦(#[0-9A-F]+)¦i",$string,$matches);

$matches[0] should be an array of all the colours used.

$colors = $matches[0];

foreach ($colors as $color) {
print "$color<P>";
}

Should get you started. If you get any bugs use print_r($matches) - to get the full data structure.

[edit - beaten to it again.... ;) anyways nice to get two of the same answer!]

ShawnR

12:38 am on May 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also remember there is a short-hand for colours (#fff is equiv to #ffffff) and there are named colours.

SinclairUser

1:44 am on May 7, 2003 (gmt 0)

10+ Year Member



Hmmm,

Even the smallest question can get complicated. I did not know that you could shorthand #FFFFFF to #FFF - you learn something new everyday.

Chris.

grahamstewart

1:55 am on May 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure if you can use shorthand in HTML but you certainly can in CSS.

raingerpete

8:18 pm on May 7, 2003 (gmt 0)

10+ Year Member



Thanks for that!

I had thought about the CSS issue and am still thinking about it. I can open a URL web page but the ways in which a CSS are specified can vary.

If it is included in <style></style> then my script will pick the colour out.

But the colour can be specified as a rel. link

<link rel="stylesheet" href="/css/default.css" type="text/css" />

or a direct link

<link rel="stylesheet" href="http://www.si.com/css/default.css" type="text/css" />

How do I open both of these types?

Pete