How to check if a string is a valid XML in PHP

1 Answer

0 votes
$xml_string = 
        "<note>
            <to>Tom</to>
            <from>R2D2</from>
            <heading>Reminder</heading>
            <body>Don't forget the Commodore 128</body>
        </note>";

$xml = simplexml_load_string($xml_string);

if ($xml === false) {
  print "Invalid XML";
} else {
  print "Valid XML";
}




/*
run:

Valid XML

*/

 



answered Dec 1, 2023 by avibootz

Related questions

1 answer 122 views
1 answer 205 views
1 answer 385 views
1 answer 287 views
...