📄 v2.php
字号:
} } $tmp .= $this->options['linebreak']; } $this->_tagDepth--; if ($this->options['indent']!==null && $this->_tagDepth>0) { $tmp .= str_repeat($this->options['indent'], $this->_tagDepth); } if (trim($tmp) === '') { $tmp = null; } $tag = array( 'qname' => $tagName, 'content' => $tmp, 'attributes' => $attributes ); } if ($this->options['typeHints'] === true) { if (!isset($tag['attributes'][$this->options['typeAttribute']])) { $tag['attributes'][$this->options['typeAttribute']] = 'array'; } } $string = $this->_createXMLTag($tag, false); return $string; } /** * create a tag from an array * this method awaits an array in the following format * array( * 'qname' => $tagName, * 'attributes' => array(), * 'content' => $content, // optional * 'namespace' => $namespace // optional * 'namespaceUri' => $namespaceUri // optional * ) * * @access private * @param array $tag tag definition * @param boolean $replaceEntities whether to replace XML entities in content or not * @return string $string XML tag */ function _createXMLTag( $tag, $replaceEntities = true ) { if ($this->options['indentAttributes'] !== false) { $multiline = true; $indent = str_repeat($this->options['indent'], $this->_tagDepth); if ($this->options['indentAttributes'] == '_auto') { $indent .= str_repeat(' ', (strlen($tag['qname'])+2)); } else { $indent .= $this->options['indentAttributes']; } } else { $multiline = false; $indent = false; } if (is_array($tag['content'])) { if (empty($tag['content'])) { $tag['content'] = ''; } } elseif(is_scalar($tag['content']) && (string)$tag['content'] == '') { $tag['content'] = ''; } if (is_scalar($tag['content']) || is_null($tag['content'])) { if ($this->options['encoding'] == 'UTF-8' && version_compare(phpversion(), '5.0.0', 'lt')) { $encoding = XML_UTIL_ENTITIES_UTF8_XML; } else { $encoding = XML_UTIL_ENTITIES_XML; } $tag = XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $this->options['linebreak'], $encoding); } elseif (is_array($tag['content'])) { $tag = $this->_serializeArray($tag['content'], $tag['qname'], $tag['attributes']); } elseif (is_object($tag['content'])) { $tag = $this->_serializeObject($tag['content'], $tag['qname'], $tag['attributes']); } elseif (is_resource($tag['content'])) { settype($tag['content'], 'string'); $tag = XML_Util::createTagFromArray($tag, $replaceEntities); } return $tag; }}//foreach (explode(PATH_SEPARATOR, ini_get('include_path')) as $path) {// $t = $path . DIRECTORY_SEPARATOR . 'XML' . DIRECTORY_SEPARATOR .// 'Util';// if (file_exists($t) && is_readable($t)) {// include_once 'XML/Util';// }//}//if (!class_exists('XML_Util')) {// well, it's one way to do things without extra deps .../* vim: set expandtab tabstop=4 shiftwidth=4: */// +----------------------------------------------------------------------+// | PHP Version 4 |// +----------------------------------------------------------------------+// | Copyright (c) 1997-2006 The PHP Group |// +----------------------------------------------------------------------+// | This source file is subject to version 2.0 of the PHP license, |// | that is bundled with this package in the file LICENSE, and is |// | available through the world-wide-web at the following url: |// | http://www.php.net/license/3_01.txt |// | If you did not receive a copy of the PHP license and are unable to |// | obtain it through the world-wide-web, please send a note to |// | license@php.net so we can mail you a copy immediately. |// +----------------------------------------------------------------------+// | Authors: Stephan Schmidt <schst@php-tools.net> |// +----------------------------------------------------------------------+//// $Id: v2.php,v 1.1.2.2 2006/01/01 13:47:00 sniper Exp $/** * error code for invalid chars in XML name */define("XML_UTIL_ERROR_INVALID_CHARS", 51);/** * error code for invalid chars in XML name */define("XML_UTIL_ERROR_INVALID_START", 52);/** * error code for non-scalar tag content */define("XML_UTIL_ERROR_NON_SCALAR_CONTENT", 60); /** * error code for missing tag name */define("XML_UTIL_ERROR_NO_TAG_NAME", 61); /** * replace XML entities */define("XML_UTIL_REPLACE_ENTITIES", 1);/** * embedd content in a CData Section */define("XML_UTIL_CDATA_SECTION", 2);/** * do not replace entitites */define("XML_UTIL_ENTITIES_NONE", 0);/** * replace all XML entitites * This setting will replace <, >, ", ' and & */define("XML_UTIL_ENTITIES_XML", 1);/** * replace only required XML entitites * This setting will replace <, " and & */define("XML_UTIL_ENTITIES_XML_REQUIRED", 2);/** * replace HTML entitites * @link http://www.php.net/htmlentities */define("XML_UTIL_ENTITIES_HTML", 3);/** * replace all XML entitites, and encode from ISO-8859-1 to UTF-8 * This setting will replace <, >, ", ' and & */define("XML_UTIL_ENTITIES_UTF8_XML", 4);/** * utility class for working with XML documents * * @category XML * @package XML_Util * @version 0.6.0 * @author Stephan Schmidt <schst@php.net> */class XML_Util { /** * return API version * * @access public * @static * @return string $version API version */ function apiVersion() { return "0.6"; } /** * replace XML entities * * With the optional second parameter, you may select, which * entities should be replaced. * * <code> * require_once 'XML/Util.php'; * * // replace XML entites: * $string = XML_Util::replaceEntities("This string contains < & >."); * </code> * * @access public * @static * @param string string where XML special chars should be replaced * @param integer setting for entities in attribute values (one of XML_UTIL_ENTITIES_XML, XML_UTIL_ENTITIES_XML_REQUIRED, XML_UTIL_ENTITIES_HTML) * @return string string with replaced chars */ function replaceEntities($string, $replaceEntities = XML_UTIL_ENTITIES_XML) { switch ($replaceEntities) { case XML_UTIL_ENTITIES_UTF8_XML: return strtr(utf8_encode($string),array( '&' => '&', '>' => '>', '<' => '<', '"' => '"', '\'' => ''' )); break; case XML_UTIL_ENTITIES_XML: return strtr($string,array( '&' => '&', '>' => '>', '<' => '<', '"' => '"', '\'' => ''' )); break; case XML_UTIL_ENTITIES_XML_REQUIRED: return strtr($string,array( '&' => '&', '<' => '<', '"' => '"' )); break; case XML_UTIL_ENTITIES_HTML: return htmlspecialchars($string); break; } return $string; } /** * build an xml declaration * * <code> * require_once 'XML/Util.php'; * * // get an XML declaration: * $xmlDecl = XML_Util::getXMLDeclaration("1.0", "UTF-8", true); * </code> * * @access public * @static * @param string $version xml version * @param string $encoding character encoding * @param boolean $standAlone document is standalone (or not) * @return string $decl xml declaration * @uses XML_Util::attributesToString() to serialize the attributes of the XML declaration */ function getXMLDeclaration($version = "1.0", $encoding = null, $standalone = null) { $attributes = array( "version" => $version, ); // add encoding if ($encoding !== null) { $attributes["encoding"] = $encoding; } // add standalone, if specified if ($standalone !== null) { $attributes["standalone"] = $standalone ? "yes" : "no"; } return sprintf("<?xml%s?>", XML_Util::attributesToString($attributes, false)); } /** * build a document type declaration * * <code> * require_once 'XML/Util.php'; * * // get a doctype declaration: * $xmlDecl = XML_Util::getDocTypeDeclaration("rootTag","myDocType.dtd"); * </code> * * @access public * @static * @param string $root name of the root tag * @param string $uri uri of the doctype definition (or array with uri and public id) * @param string $internalDtd internal dtd entries * @return string $decl doctype declaration * @since 0.2 */ function getDocTypeDeclaration($root, $uri = null, $internalDtd = null) { if (is_array($uri)) { $ref = sprintf( ' PUBLIC "%s" "%s"', $uri["id"], $uri["uri"] ); } elseif (!empty($uri)) { $ref = sprintf( ' SYSTEM "%s"', $uri ); } else { $ref = ""; } if (empty($internalDtd)) { return sprintf("<!DOCTYPE %s%s>", $root, $ref); } else { return sprintf("<!DOCTYPE %s%s [\n%s\n]>", $root, $ref, $internalDtd); } } /** * create string representation of an attribute list * * <code> * require_once 'XML/Util.php'; * * // build an attribute string * $att = array( * "foo" => "bar", * "argh" => "tomato" * ); * * $attList = XML_Util::attributesToString($att); * </code> * * @access public * @static * @param array $attributes attribute array * @param boolean|array $sort sort attribute list alphabetically, may also be an assoc array containing the keys 'sort', 'multiline', 'indent', 'linebreak' and 'entities' * @param boolean $multiline use linebreaks, if more than one attribute is given * @param string $indent string used for indentation of multiline attributes * @param string $linebreak string used for linebreaks of multiline attributes * @param integer $entities setting for entities in attribute values (one of XML_UTIL_ENTITIES_NONE, XML_UTIL_ENTITIES_XML, XML_UTIL_ENTITIES_XML_REQUIRED, XML_UTIL_ENTITIES_HTML) * @return string string representation of the attributes * @uses XML_Util::replaceEntities() to replace XML entities in attribute values * @todo allow sort also to be an options array */ function attributesToString($attributes, $sort = true, $multiline = false, $indent = ' ', $linebreak = "\n", $entities = XML_UTIL_ENTITIES_XML) { /** * second parameter may be an array */ if (is_array($sort)) { if (isset($sort['multiline'])) { $multiline = $sort['multiline']; } if (isset($sort['indent'])) { $indent = $sort['indent']; } if (isset($sort['linebreak'])) { $multiline = $sort['linebreak']; } if (isset($sort['entities'])) { $entities = $sort['entities']; } if (isset($sort['sort'])) { $sort = $sort['sort']; } else { $sort = true; } } $string = ''; if (is_array($attributes) && !empty($attributes)) { if ($sort) { ksort($attributes); } if( !$multiline || count($attributes) == 1) { foreach ($attributes as $key => $value) { if ($entities != XML_UTIL_ENTITIES_NONE) { $value = XML_Util::replaceEntities($value, $entities); } $string .= ' '.$key.'="'.$value.'"'; } } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -