I am looking for light weight tool/API/script(in PHP) to validate whether a html code sippet is 'tidy'.By 'tidy' I simply mean whether the html snippet has properly closed tags and is valid html.

the tools that are usually ailable are generally for validating a url html. I wnat to be able to validate only a small html snippet . e.g

HTML Code:
<a href="xyz.com" Click Here </a>
the above is invalid as the
HTML Code:
<a>
tag does not close properly
where as

HTML Code:
<span> my name is xyz</span>
is valid .

What would be the best way to validate html snippets? Thanks in advance for your help.