note 57378 added to ref.simplexml
| From: | explosion at explosiondigital dot com | Date: | Sat, 01 Oct 2005 23:32:50 +0000 |
| Subject: | note 57378 added to ref.simplexml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-96153@lists.php.net to get a copy of this message | ||
<?
#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;
}
?>
----
Manual Page -- http://www.php.net/manual/en/ref.simplexml.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+57378
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+57378&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+57378&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+57378&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+57378&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+57378&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+57378&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+57378&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+57378&report=yes
Search -- http://master.php.net/manage/user-notes.php