Converting & to and for XML in PHP

I am creating XML-RSS for my page. And by running this error:

error on line 39 at column 46: xmlParseEntityRef: no name

Apparently, this is because I cannot have XML as well ... What am I doing in my last line field ...

What is the best way to clean all mine $row['field']'sin PHP so that they turn into&

+3
source share
5 answers

Use htmlspecialcharsfor encoding only specific HTML symbols &, <, >, "and optionally '(cm. Second parameter $quote_style).

+8
source

Indeed, you need to look at the dom xml functions in php. Its a bit of work to understand, but you avoid such problems.

+2
source

Convert XML reserved characters to objects

 function xml_convert($str, $protect_all = FALSE)
{
    $temp = '__TEMP_AMPERSANDS__';

    // Replace entities to temporary markers so that
    // ampersands won't get messed up
    $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);

    if ($protect_all === TRUE)
    {
        $str = preg_replace("/&(\w+);/",  "$temp\\1;", $str);
    }

    $str = str_replace(array("&","<",">","\"", "'", "-"),
                        array("&amp;", "&lt;", "&gt;", "&quot;", "&apos;", "&#45;"),
                        $str);

    // Decode the temp markers back to entities
    $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);

    if ($protect_all === TRUE)
    {
        $str = preg_replace("/$temp(\w+);/","&\\1;", $str);
    }

    return $str;
}
+1
source

Use

html_entity_decode($row['field']);

It will take and go back to and from, as well as if you have & npsb; this will change it to a space.

http://us.php.net/html_entity_decode

Greetings

0
source

Source: https://habr.com/ru/post/1712691/


All Articles