Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

X/Html Validator in PHP

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)?

like image 673
Kaminari Avatar asked Feb 17 '11 14:02

Kaminari


People also ask

How do I check if HTML is valid?

The World Wide Web Consortium provide a simple online tool (https://validator.w3.org/) that automatically check your HTML code and point out any problems/errors your code might have, such as missing closing tags or missing quotes around attributes.

What is input validation in PHP?

Validation means check the input submitted by the user. There are two types of validation are available in PHP. They are as follows − Client-Side Validation − Validation is performed on the client machine web browsers.

How do I find the syntax error in HTML?

Validating our HTML: In order to make sure your html code is error free is by using html validation service by w3c. This site takes html as input and returns the errors in html document. You can provide html by providing link to html document, uploading html file or directly pasting html there.


2 Answers

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

  • DOMDocument::validate — Validates the document based on its DTD

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()

like image 101
Gordon Avatar answered Oct 17 '22 23:10

Gordon


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.

like image 44
Thomas Avatar answered Oct 17 '22 22:10

Thomas