I have a system that writes a XML file.
The problem is that I cannot have in the XML file this symbol &
I need to replace this symbol wherever it is find in the code with &
The problem is I have a LOT of strings, and I do not want to apply a function that will replace that symbol for EVERY SINGLE string I have.
This is an example:
$stringData ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
";
$stringData .="<ingrooves_import xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xsi:schemaLocation=\"http://schemas.ingrooves.com/INgrooves/INgroovesImport_1_0.xsd\" schema_version=\"1\" schema_revision=\"0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://schemas.ingrooves.com/INgrooves/INgroovesImport_1_0.xsd\">
";
$stringData .= "<album>
";
$stringData .= "<title>".$albumTitle."</title>
";
$stringData .= "<label_reference>LA0001</label_reference>
";
$stringData .= "<performer>".$MainArtist."</performer>
";
$stringData .= "<catalog_number>".$CatalogueNumber."</catalog_number>
";
$stringData .= "<upc>".$UPC_EAN_JAN."</upc>
";
$stringData .= "<release_date>".$releaseDate."</release_date>
";
$stringData .= "<copyright>".$DigiDistYear."</copyright>
";
And I have many more of $stringData in my code. So you can understand I do not want to apply for every $stringData a code that will replace that symbol.
I want to find a way to tell to the code, that for any $stringData variable, and for any content this $stringData will encounter, then it must replace &
with &
Is there a way of doing it?
In few words, the code should do so:
For every content you are going to encounter inside the variable $stringData, from now on, you should change &
with &
everytime you encounter this symbol.
Please help me!
Thanks
At the end of the script, when you finished adding code to the string, add the following:
$stringData = str_replace ('&','&',$stringData);
You can do also with regular expressions but this will work aswell :-)
This might be too far fetched... but what about simply using str_replace
after building your XML output?
$stringData = str_replace('&','&',$stringData);
Note:
This would replace even an already existing &
to &amp;
, so make sure you don't already have some &
in your string before using this line of code.
use this, and it should work fine:
$stringData = str_replace('&','&',$stringData);
I am not sure if I understood your question correctly.
Basically, at the end of the assignment, you replace all occurences of & inside stringData with &
$myString = "this is a test & you &&& can & see";
$newString = str_replace('&', '&', $myString);
echo $newString;
In the new variable, you can see that all & and repalced now with &
You can not see the results in a browser because the browser will display all & as &
You need to look at the code or test the code in a terminal.