PDA

View Full Version : How can i replace the ampersand with &amp


PibbTibbs
07-13-2007, 05:13 PM
Hi all

I have a small php script that grabs data from our sql server and outputs xml tags for each bit of information, anytime a result contains an ampersand the xml chokes, I'd like to have php replace each instance of the ampersand it sees to &amp.

I looked up str_replace function in php manual but i cannot wrap my head around it. I'd like to know if there is an easy way to make the replacements for the CustName field (only one that will contain ampersands) while the xml is being written.

here is my php so far:

<?php
$city = $_REQUEST["city"];

$server = "******";
$database = "*****";


$link = mssql_connect($server, "*****", "******");
mssql_select_db($database, $link);

$query = " SELECT CustName, Addr1, City, PostalCode, Phone, Email, Internet FROM dbo.tblArCust WHERE City = '$city' AND classId = 'GRL'
OR City= '$city' AND classId = 'WIG/GL' ORDER BY CustName ";
$results = mssql_query($query);

echo "<?xml version=\"1.0\"?>\n";
echo "<siteText>\n";

while ($line = mssql_fetch_assoc($results)) {
echo "<item>" . $line["CustName"] . "</item>\n";
echo "<item>" . $line["Addr1"] . "</item>\n";
echo "<item>" . $line["City"] . "</item>\n";
echo "<item>" . $line["PostalCode"] . "</item>\n";
echo "<item>" . $line["Phone"] . "</item>\n";
echo "<item>" . $line["Email"] . "</item>\n";
echo "<item>" . $line["Internet"] . "</item>\n";
}
echo "</siteText>\n";

mssql_close($link);

?>



Thanks for any help you can provide.

PibbTibbs
07-13-2007, 05:54 PM
Bah, major brain fart has just evacuated my melon, I figured it out.

I just had to replace first line in the while statement to this:

echo "<item>" . str_replace('&', '&amp;', $line["CustName"]) . "</item>\n";