Friday, 12 September 2014

º, aka Masculine Ordinal Indicator, Breaking XML Node Creation in PHP



Here is some PHP code that creates XML nodes from a MySQL DB call. It works great except the masculine ordinal indicator º breaks the code. Any suggestions or tips would be much appreciated. Thanks in advance!


Code



$result = mysql_query("SELECT street FROM Addresses");
while ($row = @mysql_fetch_assoc($result)){
$node = $dom->create_element("marker");
$newnode = $parnode->append_child($node);
$newnode->set_attribute("street", $row['street']);
}


The code breaks when $row['street'] has an entry that looks something like Nº111-E AVENIDA FOO.


Expected Result



<markers><marker street="Nº111-E AVENIDA FOO" /></markers>


Actual (Broken) Result



<markers><marker street="N


It breaks on the º. I've tried using utf8_encode($row['street']) and htmlentities($row['street']) but neither helped.


Manually Entering Character


The curious thing is that if the address is added manually, as follows, the code works well and in fact, the º automatically gets converted to its HTML encoding &#xBA;.



$result = mysql_query("SELECT street FROM Addresses");
while ($row = @mysql_fetch_assoc($result)){
$node = $dom->create_element("marker");
$newnode = $parnode->append_child($node);
$newnode->set_attribute("street", "Nº111-E AVENIDA FOO");
}


Result when manually entering º:



<markers><marker street="N&#xBA;111-E AVENIDA FOO" /></markers>

No comments:

Post a Comment