Nikoole
Nikoole

Reputation: 4603

XML - proper way to escape string

I just want to add in tag any string.

I am using this code to escape string:

$name = $this->_dom->createElement('name', htmlspecialchars($userName, ENT_COMPAT,'utf-8'));
$item->appendChild($name);

I got a problem, one of my users put to name field some specific symbols, and whole xml feed become broken. How i must escaping string?

Thank for help and sorry for my poor English...

Upvotes: 0

Views: 90

Answers (2)

Nikoole
Nikoole

Reputation: 4603

My solution:

$title = $this->_dom->createElement('title',
    htmlspecialchars($playlist->getTitle(), 
    ENT_DISALLOWED, 'utf-8')
);

And when you output xml, you must strip � (REPLACEMENT CHARACTER) see this related link about ENT_DISALLOWED like:

echo str_replace('�', '', $this->_dom->saveXML());

This way allows us to display any string with html entities and/or special chars.

Upvotes: 0

mwu
mwu

Reputation: 472

I think adding the xml node's value by using createTextNode instead of passing it as a parameter to createElement may solve your problem.

Upvotes: 2

Related Questions