-1

I am trying to parse through css using php I am using a css parser from this link: http://www.phpclasses.org/browse/file/4684.html, but I keep getting this error message:

Notice: Undefined offset: 1 in C:\wamp\www\Thesis\cssparser-2003-09-20\cssparser.php on line 106

The code I am using is as follows

include_once('cssparser-2003-09-20/cssparser.php');

foreach($html->find('link') as $link)
{
   $href = $link->getAttribute('href');
   $css = new cssparser();
   $css->Parse($href);
   echo $css->Get("body","color");
}

This will look for the href attribute and use the attribute from this to grab the css file which should then be parsed through but error above is occurring. Any help would be much appreciated?

BenOfTheNorth
  • 2,904
  • 1
  • 20
  • 46
Elbento
  • 11
  • 1
  • 1
  • 2

1 Answers1

1

Line 106 of cssparser.php says:

list($codekey, $codevalue) = explode(":",$code);

The explode() is generating the Undefined offset error. So that means that the css file getting parsed has in invalid statement somewhere and missing :. The explode can't find any : in $code.

Now this is just an assumption (you didn't provide actual .css file), but the file might have some invalid content, something like this:

.classdefinition {
  color #000000;
}

There's a : missing between color and #000000.

I don't think a comment is the problem, as at first look, the class takes care of skipping them.

Try passing the css file through a CSS validator.

If the CSS syntax is OK, then the class has a bug.

Now if we both have the same (latest) version of cssparser.php, a quick patch would be to replace lines 106-109 with:

$arr = explode(":", $code);
if (count($arr) == 2 && strlen(trim($arr[0])) > 0 && strlen(trim($arr[1])) > 0) {
  $this->css[$key][trim($arr[0])] = trim($arr[1]);
}

But again, that doesn't guarantee invalid CSS will be parsed correctly and that this class is error free.

And mind that I didn't actually test or worked with the class, all that is suggested here is just by looking at the code you posted and the class code.

Later edit:

At a quick google search, I found PHP-CSS-Parser which looks more complete and robust, and it's hosted on Github (so others could contribute to it).

Another edit:

Also check this answer here, looks simple enough, but as the author says, doesn't handle comments inside selectors.

Hope this helped.

Community
  • 1
  • 1
binar
  • 1,197
  • 1
  • 11
  • 24
  • Hi talereader, I was looking at and was wondering how you would grab the color from the body tag? – Elbento Mar 10 '12 at 17:15
  • Hmm, I played with it a little and can't figure out how to extract an individual selector from that object. It's too damn complex. And it looks like individual selectors is in his TO DO list. – binar Mar 10 '12 at 19:06
  • The fix you did fixed one problem but another problem occurred on line 198-200# 'foreach($parts as $part) { list($keystr,$codestr) = explode("{",$part); $keys = explode(",",trim($keystr));' – Elbento Mar 11 '12 at 10:15
  • Please post here or provide a link for the css file you are trying to parse. – binar Mar 11 '12 at 13:28