I have a problem. In my XML file are some values with names which contains:
&
How can I replace it with an normal "&" ? I tried this one but it isn't working....
It's just the name so it doesn't matter if it is double ecaped or not...
<product>
<title>Command & Conquer 3tiberium Wars</title>
<url>http://www.onlinekeystore.com/command-and-conquer-3-tiberium-wars-cd-key-origin.html</url>
<priceEUR>7.99</priceEUR>
</product>
My code who isn't working:
$string= str_replace("&", ' & ', $string);
Greetings and Thanks!
There is nothing wrong with this code
$string= str_replace("&", ' & ', $string);
Are you sure that you store the xml string in the $string variable? Can you give us more code?
My Code:
function loadTitles($tagName, $path){
$dom = new DOMDocument('1.0', 'utf-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load($path);
$marker = $dom->getElementsByTagName($tagName);
for ($i = $marker->length - 1; $i >= 0; $i--) {
$word = $marker->item($i)->textContent;
$escapedWord = escapWord($word);
$marker->item($i)->textContent = $escapedWord;
}
$dom->saveXML();
$dom->save($path);
}
function escapWord($string){
$replaceNothing = [":", ",", ";", "`", "#", "'", "´", "–", "!", "(", ")", ".", "@", "’", "+", "™"];
$replaceSpace = ["-", "–", "_", "/"];
$delete = ["Steam", "Eu", "Key"];
$string= str_replace("&", ' & ', $string);
return $string;
You're showing us the lexical XML, which contains &
, but your code is processing the DOM. The DOM presumably results from parsing the lexical XML, and one of the things the parser does is to replace &
by &
. That's not something you need to do yourself.
When you serialize/save the DOM as lexical XML then of course the process is reversed, and &
is replaced by &
. If the serializer didn't do that, then the resulting XML would be ill-formed.