50define(
'XML_UTIL_ERROR_INVALID_CHARS', 51);
55define(
'XML_UTIL_ERROR_INVALID_START', 52);
60define(
'XML_UTIL_ERROR_NON_SCALAR_CONTENT', 60);
65define(
'XML_UTIL_ERROR_NO_TAG_NAME', 61);
70define(
'XML_UTIL_REPLACE_ENTITIES', 1);
75define(
'XML_UTIL_CDATA_SECTION', 5);
80define(
'XML_UTIL_ENTITIES_NONE', 0);
86define(
'XML_UTIL_ENTITIES_XML', 1);
92define(
'XML_UTIL_ENTITIES_XML_REQUIRED', 2);
98define(
'XML_UTIL_ENTITIES_HTML', 3);
103define(
'XML_UTIL_COLLAPSE_NONE', 0);
108define(
'XML_UTIL_COLLAPSE_ALL', 1);
113define(
'XML_UTIL_COLLAPSE_XHTML_ONLY', 2);
179 switch ($replaceEntities) {
203 return htmlentities($string, ENT_COMPAT, $encoding);
251 switch ($replaceEntities) {
275 return html_entity_decode($string, ENT_COMPAT, $encoding);
300 $version =
'1.0', $encoding =
null, $standalone =
null
303 'version' => $version,
306 if ($encoding !==
null) {
307 $attributes[
'encoding'] = $encoding;
310 if ($standalone !==
null) {
311 $attributes[
'standalone'] = $standalone ?
'yes' :
'no';
339 $root, $uri =
null, $internalDtd =
null
341 if (is_array($uri)) {
342 $ref = sprintf(
' PUBLIC "%s" "%s"', $uri[
'id'], $uri[
'uri']);
343 } elseif (!empty($uri)) {
344 $ref = sprintf(
' SYSTEM "%s"', $uri);
349 if (empty($internalDtd)) {
350 return sprintf(
'<!DOCTYPE %s%s>', $root, $ref);
352 return sprintf(
"<!DOCTYPE %s%s [\n%s\n]>", $root, $ref, $internalDtd);
393 $attributes, $sort =
true, $multiline =
false,
399 if (is_array($sort)) {
400 if (isset($sort[
'multiline'])) {
401 $multiline = $sort[
'multiline'];
403 if (isset($sort[
'indent'])) {
404 $indent = $sort[
'indent'];
406 if (isset($sort[
'linebreak'])) {
407 $multiline = $sort[
'linebreak'];
409 if (isset($sort[
'entities'])) {
410 $entities = $sort[
'entities'];
412 if (isset($sort[
'sort'])) {
413 $sort = $sort[
'sort'];
419 if (is_array($attributes) && !empty($attributes)) {
423 if (!$multiline || count($attributes) == 1) {
424 foreach ($attributes as $key => $value) {
431 $string .=
' ' . $key .
'="' . $value .
'"';
435 foreach ($attributes as $key => $value) {
440 $string .=
' ' . $key .
'="' . $value .
'"';
443 $string .= $linebreak . $indent . $key .
'="' . $value .
'"';
462 if (preg_match(
'~<([^>])+/>~s', $xml, $matches)) {
471 '(https?://[^:\s]+:\w+)' .
486 return (preg_replace($preg1, $preg2, $xml)?:$xml);
491 '/<(area|base(?:font)?|br|col|frame|hr|img|input|isindex|link|meta|'
492 .
'param)([^>]*)><\/\\1>/s',
542 $qname, $attributes = array(), $content =
null,
544 $multiline =
false, $indent =
'_auto', $linebreak =
"\n",
549 'attributes' => $attributes
553 if ($content !==
null) {
554 $tag[
'content'] = $content;
558 if ($namespaceUri !==
null) {
559 $tag[
'namespaceUri'] = $namespaceUri;
563 $tag, $replaceEntities, $multiline,
564 $indent, $linebreak, $sortAttributes,
631 $multiline =
false, $indent =
'_auto', $linebreak =
"\n",
634 if (isset($tag[
'content']) && !is_scalar($tag[
'content'])) {
636 'Supplied non-scalar value as tag content',
641 if (!isset($tag[
'qname']) && !isset($tag[
'localPart'])) {
643 'You must either supply a qualified name '
644 .
'(qname) or local tag name (localPart).',
650 if (!isset($tag[
'attributes']) || !is_array($tag[
'attributes'])) {
651 $tag[
'attributes'] = array();
654 if (isset($tag[
'namespaces'])) {
655 foreach ($tag[
'namespaces'] as $ns => $uri) {
656 $tag[
'attributes'][
'xmlns:' . $ns] = $uri;
660 if (!isset($tag[
'qname'])) {
664 if (isset($tag[
'namespace']) && !empty($tag[
'namespace'])) {
665 $tag[
'qname'] = $tag[
'namespace'] .
':' . $tag[
'localPart'];
667 $tag[
'qname'] = $tag[
'localPart'];
669 } elseif (isset($tag[
'namespaceUri']) && !isset($tag[
'namespace'])) {
674 $tag[
'localPart'] = $parts[
'localPart'];
675 if (isset($parts[
'namespace'])) {
676 $tag[
'namespace'] = $parts[
'namespace'];
680 if (isset($tag[
'namespaceUri']) && !empty($tag[
'namespaceUri'])) {
682 if (isset($tag[
'namespace']) && !empty($tag[
'namespace'])) {
683 $tag[
'attributes'][
'xmlns:' . $tag[
'namespace']]
684 = $tag[
'namespaceUri'];
687 $tag[
'attributes'][
'xmlns'] = $tag[
'namespaceUri'];
691 if (!array_key_exists(
'content', $tag)) {
692 $tag[
'content'] =
'';
696 if ($multiline ===
true) {
697 if ($indent ===
'_auto') {
698 $indent = str_repeat(
' ', (strlen($tag[
'qname'])+2));
705 $sortAttributes, $multiline, $indent, $linebreak
708 switch ($replaceEntities) {
716 $tag[
'content'], $replaceEntities
721 '<%s%s>%s</%s>', $tag[
'qname'], $attList, $tag[
'content'],
725 return self::collapseEmptyTags($tag, $collapseTagMode);
753 $qname, $attributes = array(), $namespaceUri =
null,
754 $multiline =
false, $indent =
'_auto', $linebreak =
"\n",
755 $sortAttributes =
true
758 if (!isset($attributes) || !is_array($attributes)) {
759 $attributes = array();
762 if ($namespaceUri !=
null) {
767 if ($multiline ===
true) {
768 if ($indent ===
'_auto') {
769 $indent = str_repeat(
' ', (strlen($qname)+2));
773 if ($namespaceUri !=
null) {
775 if (isset($parts[
'namespace']) && !empty($parts[
'namespace'])) {
776 $attributes[
'xmlns:' . $parts[
'namespace']] = $namespaceUri;
779 $attributes[
'xmlns'] = $namespaceUri;
785 $attributes, $sortAttributes,
786 $multiline, $indent, $linebreak
788 $element = sprintf(
'<%s%s>', $qname, $attList);
809 $element = sprintf(
'</%s>', $qname);
829 $comment = sprintf(
'<!-- %s -->', $content);
851 preg_replace(
'/\]\]>/',
']]]]><![CDATA[>', strval($data))
879 if (strstr($qname,
':')) {
880 $tmp = explode(
':', $qname);
882 'namespace' => $tmp[0],
883 'localPart' => $tmp[1]
887 'namespace' => $defaultNs,
888 'localPart' => $qname
921 if (!is_string($string) || !strlen($string) || !preg_match(
'/^[[:alpha:]_]\\z/', $string[0])) {
923 'XML names may only start with letter or underscore',
930 '/^([[:alpha:]_]([[:alnum:]\-\.]*)?:)?'
931 .
'[[:alpha:]_]([[:alnum:]\_\-\.]+)?\\z/',
936 'XML names may only contain alphanumeric '
937 .
'chars, period, hyphen, colon and underscores',
959 include_once
'PEAR.php';
960 return PEAR::raiseError($msg, $code);
const XML_UTIL_COLLAPSE_NONE
const XML_UTIL_ERROR_NON_SCALAR_CONTENT
const XML_UTIL_ERROR_INVALID_CHARS
const XML_UTIL_ERROR_NO_TAG_NAME
const XML_UTIL_COLLAPSE_ALL
const XML_UTIL_COLLAPSE_XHTML_ONLY
const XML_UTIL_REPLACE_ENTITIES
const XML_UTIL_ENTITIES_XML
const XML_UTIL_ENTITIES_HTML
const XML_UTIL_ENTITIES_XML_REQUIRED
const XML_UTIL_CDATA_SECTION
const XML_UTIL_ERROR_INVALID_START
const XML_UTIL_ENTITIES_NONE
static raiseError($msg, $code)
static attributesToString( $attributes, $sort=true, $multiline=false, $indent=' ', $linebreak="\n", $entities=XML_UTIL_ENTITIES_XML)
static collapseEmptyTags($xml, $mode=XML_UTIL_COLLAPSE_ALL)
static createTag( $qname, $attributes=array(), $content=null, $namespaceUri=null, $replaceEntities=XML_UTIL_REPLACE_ENTITIES, $multiline=false, $indent='_auto', $linebreak="\n", $sortAttributes=true, $collapseTagMode=XML_UTIL_COLLAPSE_ALL)
static getDocTypeDeclaration( $root, $uri=null, $internalDtd=null)
static replaceEntities( $string, $replaceEntities=XML_UTIL_ENTITIES_XML, $encoding='ISO-8859-1')
static createComment($content)
static createStartElement( $qname, $attributes=array(), $namespaceUri=null, $multiline=false, $indent='_auto', $linebreak="\n", $sortAttributes=true)
static reverseEntities( $string, $replaceEntities=XML_UTIL_ENTITIES_XML, $encoding='ISO-8859-1')
static splitQualifiedName($qname, $defaultNs=null)
static getXMLDeclaration( $version='1.0', $encoding=null, $standalone=null)
static createCDataSection($data)
static createTagFromArray( $tag, $replaceEntities=XML_UTIL_REPLACE_ENTITIES, $multiline=false, $indent='_auto', $linebreak="\n", $sortAttributes=true, $collapseTagMode=XML_UTIL_COLLAPSE_ALL)
static isValidName($string)
static createEndElement($qname)