jcubic
jcubic

Reputation: 66470

How to add html entites to RSS feeds

I use htmlentities on data that are displaying in RSS and I have unicode characters so they are show entities like Á which service like http://feedvalidator.org/ show as invalid.

How can I add this entities to xml (like using namespace) or should I use different function to escape characters like & < >?

Upvotes: 0

Views: 1147

Answers (2)

jcubic
jcubic

Reputation: 66470

Here is function that work in php 5.3.3

function encode($string) {
    $result = '';
    foreach (str_split(utf8_decode(htmlspecialchars($string))) as $char) {
        $num = ord($char);
        if ($num > 127) {
            $result .= '&#' . $num . ';';
        } else {
            $result .= $char;
        }
    }
    return $result;
}

Upvotes: 2

Quentin
Quentin

Reputation: 943100

Use htmlspecialchars to escape characters with special meaning in XML.

Use proper character encoding for other characters.

(Skimming the documentation for htmlentities suggests that you could pass ENT_XML1 and get XML compatible numeric entities if you weren't going to use proper character encoding).

Upvotes: 2

Related Questions