X/Html Validator in PHP
Asked Answered
F

2

9

First thing: I know that there is interface to W3C validator: http://pear.php.net/package/Services_W3C_HTMLValidator/ But I don't know if I can install it on cheap hosting server. I don't think so.

I need validator for my seo tools within my Content Managment System so it must be pretty much portable.

I would love to use W3C but only if it would be portable. I can also use Curl for this but it won't be elegant solution.

The best one I found so far is: http://phosphorusandlime.blogspot.com/2007/09/php-html-validator-class.html

Is there any validator comparable to W3C but portable (only PHP that does not depend on custom packages)?

Flyboat answered 17/2, 2011 at 14:43 Comment(6)
Why can't you install it on a "cheap hosting server"?Samira
Well I loked closely to this script and it seems that it's only php and does not require custom PHP packages.Flyboat
possible duplicate of How to validate HTML/CSS in LocalhostVirtuosity
possible duplicate of Which function in PHP validate if the string is valid HTMLVirtuosity
possible duplicate of Fixing malformed HTML in PHPVirtuosity
more stackoverflow.com/search?q=validate+html+document+[php]Virtuosity
V
9

If you want to validate (X)HTML documents, you can use PHP's native DOM extension:

Example from Manual:

$dom = new DOMDocument;
$dom->load('book.xml'); // see docs for load, loadXml, loadHtml and loadHtmlFile
if ($dom->validate()) {
    echo "This document is valid!\n";
}

If you want the individual errors, fetch them with libxml_get_errors()

Virtuosity answered 17/2, 2011 at 15:26 Comment(4)
I think I tried DOMDocument::validate but had problems with it. I forget what these problems were exactly - it was a while ago now. So I set up my PHP code to access the W3C validator (just file_get_contents with the URL of the validation result page) and it worked, but nowadays for some reason the W3C server is sending a 403 error back to my PHP script. Strange - the validator is still accessible through my web browser.Creaturely
@Creaturely There is also a SOAP API to the W3 Validator nowadays: validator.w3.org/docs/api.htmlVirtuosity
@Virtuosity - This is doing the same as the regular HTML output - working through my browser but giving a 403 error when I try it through PHP.Creaturely
Just discovered that it now requires a User-Agent header to be set. So I've now got it setting one and it seems to work now.Creaturely
S
1

I asked a similar question and you might check out some of the answers there.

In summary, I would recommend either running the HTML through tidy on the host or writing a short script to validate through W3C remotely. Personally, I don't like the tidy option because it reformats your code and I hate how it puts <p> tags on every line.

Here's a link to tidy and here's a link to the various W3C validation tools.

One thing to keep in mind is that HTML validation doesn't work with server-side code; it only works after your PHP is evaluated. This means that you'd need to run your code through the host's PHP interpreter and then 'pipe' it to either the tidy utility or the remote validation service. That command would look something like:

$ php myscript.php | tidy #options go here

Personally, I eventually chose to forgo the headache and simply render the page, copy the source and validate via direct input on the W3C validation utility. There are only so many times you need to validate a page anyway and automating it seemed more trouble than it's worth.

Good luck.

Superconductivity answered 17/2, 2011 at 15:9 Comment(1)
Yes I checked all similar questions before posting but There was nothing new. Of course I know that I must validate php output but I'm pretty sure that I can simply call it by protocol for example (mysite.com/script.php) in CURL or file_get_contents. I should work fast like localhost.Flyboat

© 2022 - 2024 — McMap. All rights reserved.