A
A
Andrew2020-05-18 11:40:11
PHP
Andrew, 2020-05-18 11:40:11

How to replace $str = new SimpleXMLElement($str) in PHP 5.3?

The code $str = new SimpleXMLElement($str) throws an error, the string contains:

<div class="path">
    <a href="http://site/" class="start">Главная</a>
    <span>–</span>
    <a href="http://site/about.html">О нас</a> 
    <span>–</span>
    Схемы этажей
</div>

you need to process element by element, everything works on PHP 7.x, you need to get an object ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey, 2020-05-18
@rabamaster

helped to turn on the display of errors, an invalid character is white and an incorrect encoding, the output again had to be in a different encoding

$str = mb_convert_encoding($str, "UTF-8", "windows-1251");
    $str = str_replace("&ndash", '-', $str);

    // ОШибочки
    libxml_use_internal_errors(true);
    $xml = simplexml_load_string($str);
    if (false === $xml) {
        $errors = libxml_get_errors();
        echo 'Errors are ' . var_export($errors, true);
        throw new \Exception('invalid XML');
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question