cvs: pear /XML_Util Util.php /XML_Util/examples example.php

From: Date: Thu, 23 Dec 2004 13:22:00 +0000
Subject: cvs: pear /XML_Util Util.php /XML_Util/examples example.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-27784@lists.php.net to get a copy of this message
schst Thu Dec 23 08:22:00 2004 EDT Modified files: /pear/XML_Util Util.php /pear/XML_Util/examples example.php Log: - fixed bug with replacing HTML entities, as this used htmlspecialchars instead of htmlentities - added possibility to use different entities in createTag() http://cvs.php.net/diff.php/pear/XML_Util/Util.php?r1=1.23&r2=1.24&ty=u Index: pear/XML_Util/Util.php diff -u pear/XML_Util/Util.php:1.23 pear/XML_Util/Util.php:1.24 --- pear/XML_Util/Util.php:1.23 Fri Nov 19 14:32:53 2004 +++ pear/XML_Util/Util.php Thu Dec 23 08:21:59 2004 @@ -16,7 +16,7 @@ // | Authors: Stephan Schmidt <schst@php-tools.net> | // +----------------------------------------------------------------------+ // -// $Id: Util.php,v 1.23 2004/11/19 19:32:53 schst Exp $ +// $Id: Util.php,v 1.24 2004/12/23 13:21:59 schst Exp $ /** * error code for invalid chars in XML name @@ -46,7 +46,7 @@ /** * embedd content in a CData Section */ -define("XML_UTIL_CDATA_SECTION", 2); +define("XML_UTIL_CDATA_SECTION", 5); /** * do not replace entitites @@ -141,7 +141,7 @@ '"' => '&quot;' )); break; case XML_UTIL_ENTITIES_HTML: - return htmlspecialchars($string); + return htmlentities($string); break; } return $string; @@ -185,7 +185,7 @@ '&quot;' => '"' )); break; case XML_UTIL_ENTITIES_HTML: - $arr = array_flip(get_html_translation_table(HTML_SPECIALCHARS)); + $arr = array_flip(get_html_translation_table(HTML_ENTITIES)); return strtr($string, $arr); break; } @@ -321,6 +321,9 @@ if( !$multiline || count($attributes) == 1) { foreach ($attributes as $key => $value) { if ($entities != XML_UTIL_ENTITIES_NONE) { + if ($entities === XML_UTIL_CDATA_SECTION) { + $entities = XML_UTIL_ENTITIES_XML; + } $value = XML_Util::replaceEntities($value, $entities); } $string .= ' '.$key.'="'.$value.'"'; @@ -456,8 +459,8 @@ */ function createTagFromArray($tag, $replaceEntities = XML_UTIL_REPLACE_ENTITIES, $multiline = false, $indent = "_auto", $linebreak = "\n" ) { - if (isset($tag["content"]) && !is_scalar($tag["content"])) { - return XML_Util::raiseError( "Supplied non-scalar value as tag content", XML_UTIL_ERROR_NON_SCALAR_CONTENT ); + if (isset($tag['content']) && !is_scalar($tag['content'])) { + return XML_Util::raiseError( 'Supplied non-scalar value as tag content', XML_UTIL_ERROR_NON_SCALAR_CONTENT ); } if (!isset($tag['qname']) && !isset($tag['localPart'])) { @@ -504,16 +507,21 @@ } // create attribute list - $attList = XML_Util::attributesToString($tag["attributes"], true, $multiline, $indent, $linebreak ); - if (!isset($tag["content"]) || (string)$tag["content"] == '') { - $tag = sprintf("<%s%s />", $tag["qname"], $attList); + $attList = XML_Util::attributesToString($tag['attributes'], true, $multiline, $indent, $linebreak, $replaceEntities ); + if (!isset($tag['content']) || (string)$tag['content'] == '') { + $tag = sprintf('<%s%s />', $tag['qname'], $attList); } else { - if ($replaceEntities == XML_UTIL_REPLACE_ENTITIES) { - $tag["content"] = XML_Util::replaceEntities($tag["content"]); - } elseif ($replaceEntities == XML_UTIL_CDATA_SECTION) { - $tag["content"] = XML_Util::createCDataSection($tag["content"]); + switch ($replaceEntities) { + case XML_UTIL_ENTITIES_NONE: + break; + case XML_UTIL_CDATA_SECTION: + $tag['content'] = XML_Util::createCDataSection($tag['content']); + break; + default: + $tag['content'] = XML_Util::replaceEntities($tag['content'], $replaceEntities); + break; } - $tag = sprintf("<%s%s>%s</%s>", $tag["qname"], $attList, $tag["content"], $tag["qname"] ); + $tag = sprintf('<%s%s>%s</%s>', $tag['qname'], $attList, $tag['content'], $tag['qname'] ); } return $tag; } http://cvs.php.net/diff.php/pear/XML_Util/examples/example.php?r1=1.11&r2=1.12&ty=u Index: pear/XML_Util/examples/example.php diff -u pear/XML_Util/examples/example.php:1.11 pear/XML_Util/examples/example.php:1.12 --- pear/XML_Util/examples/example.php:1.11 Thu Oct 28 12:43:58 2004 +++ pear/XML_Util/examples/example.php Thu Dec 23 08:22:00 2004 @@ -2,7 +2,7 @@ /** * several examples for the methods of XML_Util * - * $Id: example.php,v 1.11 2004/10/28 16:43:58 schst Exp $ + * $Id: example.php,v 1.12 2004/12/23 13:22:00 schst Exp $ * * @author Stephan Schmidt * @package XML_Util @@ -181,6 +181,19 @@ print "\n<br><br>\n"; /** + * creating an XML tag with a CData Section + */ + $tag = array( + "qname" => "foo", + "attributes" => array( "key" => "value", "argh" => "tütü" ), + "content" => "Also XHTML-tags can be created and HTML entities can be replaced Ä ä Ü ö <>." + ); + + print "creating a tag with HTML entities:<br>\n"; + print htmlentities(XML_Util::createTagFromArray($tag, XML_UTIL_ENTITIES_HTML)); + print "\n<br><br>\n"; + + /** * creating an XML tag with createTag */ print "creating a tag with createTag:<br>";

« previous php.pear.cvs (#27784) next »