can i validate xhtml programmatically from a php script?

你离开我真会死。 提交于 2019-12-11 09:09:37

问题


I would like to have a PHP function to check if a URL returns valid HTML or NOT, and returns true or false.

Something like:

if (validate_page("/somefile.html")) { echo "This page validated!!"; }

I found TWINE but it doesn't just give me true or false. Also I got an error running it on my system. http://twineproject.sourceforge.net/

I found this offline tool that looked promising. http://htmlhelp.com/tools/validator/offline/

Also I found this thread that talks about a gem, but it sounds problematic. How do I validate XHTML with nokogiri?


回答1:


You can use W3C's validator API. There's a PHP library available through PEAR (click here) which uses said API.

You can also install the validator on your local server (instructions here), though you might not have sufficient permissions to do so if you are using shared hosting.




回答2:


Tidy?

Validate: http://us.php.net/manual/en/function.tidy-diagnose.php
Repair: http://us.php.net/manual/en/tidy.repairstring.php




回答3:


You could also try DOMDocument->validate() if you are using PHP 5 and if the document contains a DTD.

http://www.php.net/manual/en/domdocument.validate.php




回答4:


xhtml hast to be valid xml - if you only want to check that, you could easily use simplexml, but if you also want to check for correct elements/attributes this won't help you (in that case, NullUserExceptions hint to W3C's validator API would be the best solution to choose).




回答5:


libxml_use_internal_errors ( true );
$doc = new DOMDocument;
$doc -> loadHTMLFile ( $file ); // load the file you want validated
var_dump ( libxml_get_errors () );


来源:https://stackoverflow.com/questions/3714819/can-i-validate-xhtml-programmatically-from-a-php-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!