3 * DOMIT! Doctor is a set of utilities for repairing malformed XML
4 * @package domit-xmlparser
5 * @copyright (C) 2004 John Heinstein. All rights reserved
6 * @license http://www.gnu.org/copyleft/lesser.html LGPL License
7 * @author John Heinstein <johnkarl@nbnet.nb.ca>
8 * @link http://www.engageinteractive.com/domit/ DOMIT! Home Page
9 * DOMIT! is Free Software
13 * A (static) class containing utilities for repairing malformed XML
15 * @package domit-xmlparser
16 * @author John Heinstein <johnkarl@nbnet.nb.ca>
21 * Looks for illegal ampersands and converts them to entities
22 * @param string The xml text to be repaired
23 * @return string The repaired xml text
25 function fixAmpersands($xmlText) {
26 $xmlText = trim($xmlText);
32 $startIndex = strpos($xmlText, $illegalChar, ($startIndex +
1));
34 if ($startIndex !== false) {
35 $xmlText = domit_doctor
::evaluateCharacter($xmlText,
36 $illegalChar, ($startIndex +
1));
47 * Evaluates whether an ampersand should be converted to an entity, and performs the conversion
48 * @param string The xml text
49 * @param string The (ampersand) character
50 * @param int The character index immediately following the ampersand in question
51 * @return string The repaired xml text
53 function evaluateCharacter($xmlText, $illegalChar, $startIndex) {
54 $total = strlen($xmlText);
55 $searchingForCDATASection = false;
57 for ($i = $startIndex; $i < $total; $i++
) {
58 $currChar = substr($xmlText, $i, 1);
60 if (!$searchingForCDATASection) {
70 $searchingForCDATASection = true;
81 return (substr_replace($xmlText, '&',
82 ($startIndex - 1) , 1));