bharris Posted October 9, 2009 Share Posted October 9, 2009 I am building a table of links. I would like each cell of the table to have the same background color (no images, just standard colors) of the page that is linked to in that cell. Seems fairly simple, read the background color of the body tag on the referred to page and plug that value into the background color property of the cell. I am just not skilled enough yet (fairly new to PHP). On the surface, it seems fairly straightforward. Is it? Thanks Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 9, 2009 Share Posted October 9, 2009 Actually that's in no way straightforward. There are a number of ways in which the background color may be set. You'd have to consider the bgcolor attribute on the body tag. You'd also have to consider CSS and the way selectors are given weight according to the CSS specification as well as the fact that numerous stylesheets may have conflicting information, and in that case you'll have to figure out which one should be the correct one. Quote Link to comment Share on other sites More sharing options...
bharris Posted October 9, 2009 Author Share Posted October 9, 2009 In my case, all of the pages that I am linking to are using the background property on the body tag. So, if I can do this without considering CSS and just looking at the body tag, is it still that complicated? Thanks! Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 9, 2009 Share Posted October 9, 2009 In that case you can do something like this (assuming $source holds the HTML source code of that page): preg_match('#<body[^>]+bgcolor=[\'"]([#a-z0-9]+)[\'"][^>]*>#i', $source, $matches); $color = $matches[1]; Not tested. Quote Link to comment Share on other sites More sharing options...
bharris Posted October 9, 2009 Author Share Posted October 9, 2009 It is throwing an error but I do understand what you are doing and that helps a bunch. I just need to look at the syntax. I really appreciate your help! Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted October 10, 2009 Share Posted October 10, 2009 Ah sorry here you go: preg_match('#<body[^>]+bgcolor=[\'"]([\#a-z0-9]+)[\'"][^>]*>#i', $source, $matches); $color = $matches[1]; I forgot to escape the # inside the regex. Quote Link to comment Share on other sites More sharing options...
bharris Posted October 11, 2009 Author Share Posted October 11, 2009 This is exciting.....It is working. I used preg_match('/body bgcolor=\'([#a-z0-9]+)[\']/', $source, $matches); Worked perfectly! Thank you for the help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.