Files
@ f43d1a4680a9
Branch filter:
Location: hot67beta/libraries/domit/xml_domit_parseattributes.php
f43d1a4680a9
3.7 KiB
text/x-php
menubar 0 to 10
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | <?php
/**
* parseAttributes is a function for parsing attribute and attribute-like strings
* @package domit-xmlparser
* @copyright (C) 2004 John Heinstein. All rights reserved
* @license http://www.gnu.org/copyleft/lesser.html LGPL License
* @author John Heinstein <johnkarl@nbnet.nb.ca>
* @link http://www.engageinteractive.com/domit/ DOMIT! Home Page
* DOMIT! is Free Software
**/
/** attribute parse state, just before parsing an attribute */
define('DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE', 0);
/** attribute parse state, parsing an attribute key */
define('DOMIT_ATTRIBUTEPARSER_STATE_ATTR_KEY', 1);
/** attribute parse state, parsing an attribute value */
define('DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE', 2);
/**
*@global Array Translation table for predefined XML entities
*/
$GLOBALS['DOMIT_PREDEFINED_ENTITIES'] = array('&' => '&', '<' => '<', '>' => '>',
'"' => '"', "'" => ''');
/**
* Parses the attributes string into an array of key / value pairs
* @param string The attribute text
* @return Array An array of key / value pairs
*/
function parseAttributes($attrText, $convertEntities = true, $definedEntities = null) {
$attrText = trim($attrText);
$attrArray = array();
$maybeEntity = false;
$total = strlen($attrText);
$keyDump = '';
$valueDump = '';
$currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE;
$quoteType = '';
if ($definedEntities == null) $defineEntities = array();
for ($i = 0; $i < $total; $i++) {
// $currentChar = $attrText{$i};
$currentChar = substr($attrText, $i, 1);
if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE) {
if (trim($currentChar != '')) {
$currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_KEY;
}
}
switch ($currentChar) {
case "\t":
if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
$valueDump .= $currentChar;
}
else {
$currentChar = '';
}
break;
case "\x0B": //vertical tab
case "\n":
case "\r":
$currentChar = '';
break;
case '=':
if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
$valueDump .= $currentChar;
}
else {
$currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE;
$quoteType = '';
$maybeEntity = false;
}
break;
case '"':
if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
if ($quoteType == '') {
$quoteType = '"';
}
else {
if ($quoteType == $currentChar) {
if ($convertEntities && $maybeEntity) {
$valueDump = strtr($valueDump, DOMIT_PREDEFINED_ENTITIES);
$valueDump = strtr($valueDump, $definedEntities);
}
$attrArray[trim($keyDump)] = $valueDump;
$keyDump = $valueDump = $quoteType = '';
$currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE;
}
else {
$valueDump .= $currentChar;
}
}
}
break;
case "'":
if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_VALUE) {
if ($quoteType == '') {
$quoteType = "'";
}
else {
if ($quoteType == $currentChar) {
if ($convertEntities && $maybeEntity) {
$valueDump = strtr($valueDump, $predefinedEntities);
$valueDump = strtr($valueDump, $definedEntities);
}
$attrArray[trim($keyDump)] = $valueDump;
$keyDump = $valueDump = $quoteType = '';
$currentState = DOMIT_ATTRIBUTEPARSER_STATE_ATTR_NONE;
}
else {
$valueDump .= $currentChar;
}
}
}
break;
case '&':
//might be an entity
$maybeEntity = true;
$valueDump .= $currentChar;
break;
default:
if ($currentState == DOMIT_ATTRIBUTEPARSER_STATE_ATTR_KEY) {
$keyDump .= $currentChar;
}
else {
$valueDump .= $currentChar;
}
}
}
return $attrArray;
} //parseAttributes
?>
|