+ /**
+ * Take an associative array of attribute name/value pairs
+ * and generate a css style representing all the style-related
+ * attributes. If there already a style attribute in the array,
+ * it is also included in the value returned.
+ */
+ static function styleFromAttributes( $attributes ) {
+ $styles = array();
+
+ foreach ( $attributes as $attribute => $value ) {
+ if ( $attribute == 'bgcolor' ) {
+ $styles[] = "background-color: $value";
+ } else if ( $attribute == 'border' ) {
+ $styles[] = "border-width: $value";
+ } else if ( $attribute == 'align' ) {
+ $styles[] = "text-align: $value";
+ } else if ( $attribute == 'valign' ) {
+ $styles[] = "vertical-align: $value";
+ } else if ( $attribute == 'width' ) {
+ if ( preg_match( '/\d+/', $value ) === false ) {
+ $value .= 'px';
+ }
+
+ $styles[] = "width: $value";
+ } else if ( $attribute == 'height' ) {
+ if ( preg_match( '/\d+/', $value ) === false ) {
+ $value .= 'px';
+ }
+
+ $styles[] = "height: $value";
+ } else if ( $attribute == 'nowrap' ) {
+ if ( $value ) {
+ $styles[] = "white-space: nowrap";
+ }
+ }
+ }
+
+ if ( isset( $attributes[ 'style' ] ) ) {
+ $styles[] = $attributes[ 'style' ];
+ }
+
+ if ( !$styles ) return '';
+ else return implode( '; ', $styles );
+ }
+