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

simply i have string like:

<h2 Article name</h2>This is an idea.<ul style="color:green;"><li>option 1</li><li>option 2</li><li>option 1<li></ul>

As you can see it is not valid, how can symfony or just PHP detect this and return where the error is?

I have found the following code:

private function validateHTML($string)
{
    preg_match_all('#<(?!meta|img|br|hr|inputb)b([a-z]+)(?: .*)?>#iU', $string, $result);
    $openedTags = $result[1];
    preg_match_all('#</([a-z]+)>#iU', $string, $result);
    $closedTags = $result[1];
    $len_opened = count($openedTags);

    return (count($closedTags) == $len_opened) ? $string : false;
}

but it works sometimes good, but not for the example i had noticed at the top for example.

I am using symfony 4.2

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *