Which Function in PHP Validate If the String Is Valid HTML

Which function in php validate if the string is valid html?

Maybe you need to check if the string is well formed.

I would use a function like this

function check($string) {
$start =strpos($string, '<');
$end =strrpos($string, '>',$start);

$len=strlen($string);

if ($end !== false) {
$string = substr($string, $start);
} else {
$string = substr($string, $start, $len-$start);
}
libxml_use_internal_errors(true);
libxml_clear_errors();
$xml = simplexml_load_string($string);
return count(libxml_get_errors())==0;
}

Just a warning: html permits unbalanced string like the following one. It is not an xml valid chunk but it is a legal html chunk

<ul><li>Hi<li> I'm another li</li></ul>

Disclaimer I've modified the code (without testing it). in order to detect well formed html inside the string.

A last though
Maybe you should use strip_tags to control user input (As I've seen in your comments)

Is there a way to detect if string have valid HTML syntax in PHP or symfony?

I think the best way is render it and then check the error of it
test this code:

$html="<html><body><p>This is array.</p><br></body></html>";

libxml_use_internal_errors(true);

$dom = New DOMDocument();
$dom->loadHTML($html);

if (empty(libxml_get_errors())) {
echo "This is a good HTML";
} else {
echo "This not html";
}

output

This is a good HTML

Other Way

you can use simplexml_load_string to validate your html too, like this example:

function check($string){
$start = strpos($string, '<');
$end = strrpos($string, '>', $start);

if ($end !== false) {
$string = substr($string, $start);
} else {
$string = substr($string, $start, strlen($string) - $start);
}

$string = "<div>$string</div>";

libxml_use_internal_errors(true);
libxml_clear_errors();
simplexml_load_string($string);

return count(libxml_get_errors()) == 0;
}

$html="<html><body><p>This is array.</p></body></html>";

if (check($html)) {
echo "This is a good HTML";
} else {
echo "This not html";
}

But this way has a one problem and for example if you have a <br> tag in your code it return the false so I recommend to use the first way which is better

X/Html Validator in PHP

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

how to check if string is valid XHTML using PHP?

This code is a correct HTML but is NOT correct as XML, because empty-element tag, such as <br /> in XML must used only between <section></section>

So you can use $dom -> loadHTML($html); or remove <be /> from your HTML and then use $dom -> loadXML($html);

PHP - If string contains '' or '' - strpos()

if($string != strip_tags($string)) {
// contains HTML
}

Took the answer from here

How to check if string contents have any HTML in it?

If you want to test if a string contains a "<something>", (which is lazy but can work for you), you can try something like that :

function is_html($string)
{
return preg_match("/<[^<]+>/",$string,$m) != 0;
}


Related Topics



Leave a reply



Submit