Parsing CSS using PHP error undefined offset 1 - php

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?

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.

Related

Row segments in odtphp

I'm currently generating some invoices with odtphp. (https://github.com/cybermonde/odtphp)
I made a segment that contains a table which will be dynamic (and therefore also use a segment) so i use the [!-- BEGIN row.segmentname --] and [!-- END row.segmentname --] tags.
My first segment is working but the row segment won't work.
Here's the error it prints:
Fatal error: Uncaught exception 'OdfException' with message ''details' segment not found in the document
Here's the part of the code that's not working:
Has anyone experienced this?
while ($row = mysql_fetch_array($res))
{
$segment->setVars("codearticle", $row['codearticle']);
$segment->setVars("designation2", $row['designation']);
$segment->setVars("qte", $row['quantite']);
$segment->setVars("prixvente", $row['prixvente']);
$segment->setVars("totalpiece", $row['montantvente']);
$segment->merge();
}
$odf->mergesegment($segment);
What should I do to make this work?
So, after a while of fiddling with my php code, i opened content.xml (from the odt archive) with my text editor and saw that some parts of the text were separated by some xml markups.
I edited it and it seems to find the row tag.
I still have some minor problems, it won't get into my while loop, but at least this part works!

XML / PHP : Creation of a XML text with PHP

I'm a beginner with XML on PHP. My page returns me the following PHP error:
But, at line 2, there only is a "require_once", followed by 3 others "require_once", the path to the required file is ok, and it ends with a ';'. The line above is the "
I have this sample of code, which I think is the source of this error (in this order)
$xml = simplexml_load_string("<result/>");
$entitlements = $xml->addChild("entitlements");
$entitlements->addChild("productId", $productId);
then, below :
$fulfillmentXML = new SimpleXMLElement($result);
//some stuff with $fullfillmentXML
echo $xml->asXML();
I do not understand exactly how the XML works in PHP, but I thought there was a problem creating a SimpleXMLElement() after the simplexml_load_string() call.
Your error doesn't look like a PHP error, but rather a browser error generated when it tried to parse the supplied XML contents. Therefore line 2 of the error does not refer to line 2 of your PHP file, but to line 2 of your generated output.
Check your output (source of your generated page), and see what line 2 is there. Make sure you only have 1 root element, and that nothing comes after it.

Problems using PHP variables with the same CSS selector names in newer PHP versions

I'm working on an e-commerce site as part of an online PHP class at Treehouse.com. I ran into an issue where one of the menu items was displaying incorrectly as can be seen here: http://herkuhleez.com/shirts4mike/
Upon looking for an answer in their forums, another student concluded it was an issue with the newer versions of PHP as the older versions had no display issues with the exact same code as seen here: http://cheetahcandy.com/shirts4mike/
The first example (with the error) is running PHP version 5.4.24 and the second one is running 5.2.17. My localhost server is running 5.5.6 and I have the same error as the first example.
This student however discovered a work around in newer versions by simply changing any php variable names that are the same as the css selector names, at least when the php is written inside a tag's class attribute.
My QUESTION is: Is this a bug in these newer versions of PHP or is this working as intended as some sort of auto-correct function?
EDIT* Here is the forum discussion over at Treehouse: https://teamtreehouse.com/forum/build-a-simple-php-application-adding-active-states-to-the-navigation-php-versions
If you have a look at your CSS on the site with the error, you will notice that your class .section.shirts has the following css on line 605 of style.css
padding-bottom: 42px;
background: #fff
Funnily enough if you take both of those out, it looks exactly the same as the other website.
In addition, the class also has an error in it:
shirts <br /> <b>Notice</b>: Undefined variable: section in <b>/home4/herkuhle/public_html/shirts4mike/inc/header.php</b> on line <b>17</b><br />
I would have a look at header.php on line 17 to see what is causing that.
MORE INFORMATION
EDIT: This question seems to relate to a predefined project available at an educational site. The OP seems to be confused as to why the exact same code fails on some servers, and seems to work others. They were thinking it was something to do with the PHP version. It is more likely as a result of the suppression of warnings server side. To suppress warnings you could simply add:
error_reporting(E_ERROR | E_PARSE);
However; it would be far better to define or test the variable properly [in this case $section] and not simply mask the issue.
Using a PHP variable called $section along with a CSS selector of the same name will work
..Just because you can do something, doesn't mean you should..
Anyway, it is not a CSS error per se, but it certainly doesn't help in this circumstance.
Take note that the error/warning in your code has the word section in it:
<li class="shirts <br /> <b>Notice</b>: Undefined variable: section .....">
Both examples have a CSS style called section.shirts:
.section.shirts {
background: none repeat scroll 0 0 #FFFFFF;
padding-bottom: 42px;
}
As a result of the placement of the error/warning, most browsers will interpret your class as to include any of the words found within the quotes (eg; shirts undefined section etc) and apply them all as styles if found in your CSS. In your version of the page, as a result of the placement of the error, you are applying the styling above. The other site does not have an error/warning with the word section within double quotes inside a class so it does not apply that particular styling.
There is no correlation between PHP variables and CSS selectors (read: There is no conflict between PHP Variables and CSS Selector Names/variables in current, older or dare I say it, future versions of PHP) with the same name unless you generate an error in your resultant HTML in an unfortunate position. In this case, you have done this by creating an error between double quotes inside a class.
Fix your error and your styling will apply as expected. No bug here (other than line 17 in your code :)
Without seeing your code, to fix your error/warning:
[Unlikely] Maybe add a $ in front of section where applicable in your code if it is missing somewhere (like this $section)
Maybe ensure you are defining $section before you test it in your if statements
Maybe Use isset when testing $section if you don't know if it was previously initialized.
So in summary, the word section is rendered inside your HTML within an error/warning and interpreted as part of your CSS class structure as discussed.
Why does it work on older versions of PHP?
If you are wondering why it works in other environments, have you considered that maybe it has nothing to do with the PHP Version - Perhaps in other environments with the same error/warning, they have configured PHP to suppress the display of errors and or warnings.
Without the error, the word section would not appear inside your class; ergo - no style issue.
From the PHP Manual:
Relying on the default value of an uninitialized variable is
problematic in the case of including one file into another which uses
the same variable name. It is also a major security risk with
register_globals turned on. E_NOTICE level error is issued in case of
working with uninitialized variables, however not in the case of
appending elements to the uninitialized array. isset() language
construct can be used to detect if a variable has been already
initialized.
For more information about your particular error, see this previous answer.
Just have a look in your HTML markup and you 'll recognize the following php notice:
<b>Notice</b>: Undefined variable: section in <b>/home4/herkuhle/public_html/shirts4mike/inc/header.php</b> on line <b>17</b>
Just have a look in your header.php file around line 17. Your problem is a follow up of this php notice. Looks like a css class isn 't defined properly.

xml parsing error, using php's asxml()

$file = simplexml_load_file($url); {
foreach($file->entry as $post) {
$row = simplexml_load_string($post->asXML()); // after adding this line, i get error message
$links = $row->xpath('//link[#rel="alternate" and #type="text/html"]');
echo (string) $post->title;
echo (string) $links[0]['href'];
I use this script to parse atom feed. At first didn't work because it couldn't pass the link's href attribute properly. I added $row and even though it worked, it gives an error : "namespace prefix gd for etag on entry is not defined". I'm searching this for hours, can't find a solution. I was so close.
The line $row = simplexml_load_string($post->asXML());, if it worked, would be a long-winded way of writing $row = $post. ->asXML() and simplexml_load_string perform the opposite action to each other so you'd get back the same object you started with.
I think the reason it's behaving strangely in your case is that your XML document is using "namespaces", and the fragment of XML produced by $post->asXML() doesn't quite work as an XML document on its own.
I suspect that the original problem you had, which this line seemed to magically fix, was also with namespaces, as XPath is rather sensitive to them. Look up examples of using registerXPathNamespace and see if they solve your problem. If not, feel free to post a follow-up question showing your original problem, and including a sample of the XML you're processing.

CSS issue when using PHP

What is the error in this CSS class?
.ux-row-action-cell .x-grid3-cell-inner {
padding:1px 0 0 0;
}
I don't see any error in ASP.NET, but when I am using the same CSS in PHP, Firebug says "syntax error".
Perhaps your selector is what's causing the issue... Try adding a comma in between:
.ux-row-action-cell, .x-grid3-cell-inner
It would be helpful if we could get a bigger snippet of the code around that line. Static strings would be output the same either way, so possibly a malformed piece of your dynamic code is causing the syntax error. The syntax for that one line is correct.

Categories