Analizando CSS usando PHP error undefined offset 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?

preguntado el 10 de marzo de 12 a las 15:03

Error indicates the problem is in cssparser.php. How about posting que code instead? :) -

1 Respuestas

Línea 106 de cssparser.php dice:

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

El explode() está generando el Desplazamiento indefinido 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;
}

Hay una : falta entre color y #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 Validador de CSS.

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.

Edición posterior:

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

Otra edición:

También verifica esta respuesta here, looks simple enough, but as the author says, doesn't handle comments inside selectors.

Espero que esto ayudó.

contestado el 23 de mayo de 17 a las 12:05

Hi talereader, I was looking at <github.com/sabberworm/PHP-CSS-Parser> and was wondering how you would grab the color from the body tag? - Elbento

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. - binario

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

Please post here or provide a link for the css file you are trying to parse. - binario

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.