Home > Back-end >  simplexml_load_string(): Entity: line 2: parser error : Unescaped '<' not allowed in at
simplexml_load_string(): Entity: line 2: parser error : Unescaped '<' not allowed in at

Time:10-10

First of all, I searched for this problem on the internet and could not find a solution, although there are people who have faced the same problem.

My error is: simplexml_load_string(): Entity: line 2: parser error : Unescaped '<' not allowed in attributes values

My problem is that I get this error while reading an xml file from e-commerce site with PHP. Below you can see the xml line where the error is occured.

<Product Id="" ModelCode="" Sku="" Gtin="" Name="" ShortDescription="" FullDescription="<p>XXXXXXXXXX<br/>XXXXXXXX<br/>XXXXXXXX</p>">

I think the problem is caused by <p> and <br> tags in FullDescription field but I do not know how to solve this problem. Can you help me ?

CodePudding user response:

You need to escape HTML tags in FullDescription attribute

<Product Id="" ModelCode="" Sku="" Gtin="" Name="" ShortDescription="" FullDescription="&lt;p&gt;XXXXXXXXXX&lt;br/&gt;XXXXXXXX&lt;br/&gt;XXXXXXXX&lt;/p&gt;">

Use the code below to fix all attributes including HTML tags

$xml = preg_replace_callback(
        '/="([^"]*<[^"]*)"/',
        function($m) {
            return '="'.htmlspecialchars($m[1]).'"';
        },
        $xml);
  • Related