Function that checks if the tags are correct

I have a text:

<p><div> text </div><p>

and the task is to return true if the tags are correctly set.

if for example:

<div><div><p>test<div><div><br>

but here are set incorectly and the function sholud return false;

how to make it?