note 57378 deleted from ref.simplexml by derick
| From: | derick@php.net | Date: | Thu, 13 Oct 2005 13:42:21 +0000 |
| Subject: | note 57378 deleted from ref.simplexml by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-96946@lists.php.net to get a copy of this message | ||
Note Submitter: explosion at explosiondigital dot com
----
<?
#Author: Thiago Henrique Ramos da Mata
/*
I have a found this bug in the xml functions of PHP 5 (only, where i have tested). When i try to
convert a xml to a object or to a array through XmlToArray function.
This is a workaround to "solve" this problem. But remember, when you change your current
version (with bug)
to some one (without it), you will have to remove every calls to this "temporary"
solution.
*/
$myXml = <<<Xml
<peoples>
<people name="Jônhy"/>
<people name="Whéylsy"/>
</peoples>
Xml;
$objXmlofSearch = simplexml_load_string($myXml);
foreach($objXmlofSearch->people as $people)
{
print "Without the function: ".$people['name']."<br/>\n"; #
print "With the function:
".specialCharXmlConversor($people['name'])."<br/>\n";
}
# out
#Without the function: Jônhy
#With the function: Jônhy
#Without the function: Whéylsy
#With the function: Whéylsy
/**
* Temporary function to solve a problem of conversion of XML in PHP 5
* Dangerous in a some PHP version without the bug
* will change strings with the char(195)
* @author Thiago Mata
* @since 2005-08-01
* @version 1.0
* @param a string with a wrong conversion
* @return a string with wirte chars
*/
function specialCharXmlConversor($xmlString)
{
$charBadChar = chr(195);
$pos = strpos($xmlString,$charBadChar);
$lastpos = $pos;
while ($pos !== false)
{
$before = substr($xmlString,0,$pos);
$middle = substr($xmlString,$pos+1,1);
$after = substr($xmlString,$pos+2);
$num = ord($middle);
$middle = chr($num+64);
$xmlString = $before.$middle.$after;
$text = substr($xmlString,$pos+1);
$newPos = strpos($text,$charBadChar);
if ($newPos !== false)
{
$pos += $newPos + 1;
}
else
{
$pos = false;
}
}
return $xmlString;
}
?>