Not sure if this interest anyone, but the attached patch adds a function
called var_dump_html() that performs the same functionality as
var_dump(), except that it outputs HTML instead of plain text.
var_dump() is a bit impractical to use in a web environment, IMO.

Cheers,


Marco

-- 

Marco Tabini
President

Marco Tabini & Associates, Inc.
28 Bombay Avenue
Toronto, ON M3H 1B7
Canada

Phone: (416) 630-6202
Fax: (416) 630-5057
Web: http://www.tabini.ca
Index: basic_functions.c
===================================================================
RCS file: /repository/php-src/ext/standard/basic_functions.c,v
retrieving revision 1.616
diff -u -r1.616 basic_functions.c
--- basic_functions.c	28 Jun 2003 05:38:52 -0000	1.616
+++ basic_functions.c	3 Jul 2003 14:53:08 -0000
@@ -556,6 +556,7 @@
 	PHP_FE(unserialize,														NULL)
 
 	PHP_FE(var_dump,														NULL)
+	PHP_FE(var_dump_html,														NULL)
 	PHP_FE(var_export,														NULL)
 	PHP_FE(debug_zval_dump,														NULL)
 	PHP_FE(print_r,															NULL)
Index: php_var.h
===================================================================
RCS file: /repository/php-src/ext/standard/php_var.h,v
retrieving revision 1.26
diff -u -r1.26 php_var.h
--- php_var.h	10 Jun 2003 20:03:38 -0000	1.26
+++ php_var.h	3 Jul 2003 14:53:08 -0000
@@ -24,6 +24,7 @@
 #include "ext/standard/php_smart_str_public.h"
 
 PHP_FUNCTION(var_dump);
+PHP_FUNCTION(var_dump_html);
 PHP_FUNCTION(var_export);
 PHP_FUNCTION(debug_zval_dump);
 PHP_FUNCTION(serialize);
@@ -32,7 +33,7 @@
 PHP_FUNCTION(memory_get_usage);
 #endif
 
-PHPAPI void php_var_dump(zval **struc, int level TSRMLS_DC);
+PHPAPI void php_var_dump(zval **struc, int level TSRMLS_DC, int output_html);
 PHPAPI void php_var_export(zval **struc, int level TSRMLS_DC);
 PHPAPI void php_debug_zval_dump(zval **struc, int level TSRMLS_DC);
 
Index: var.c
===================================================================
RCS file: /repository/php-src/ext/standard/var.c,v
retrieving revision 1.167
diff -u -r1.167 var.c
--- var.c	10 Jun 2003 20:03:39 -0000	1.167
+++ var.c	3 Jul 2003 14:53:09 -0000
@@ -33,8 +33,10 @@
 #include "php_smart_str.h"
 #include "basic_functions.h"
 #include "php_incomplete_class.h"
+#include "html.h"
 
 #define COMMON ((*struc)->is_ref ? "&" : "")
+#define COMMON_HTML_SAFE ((*struc)->is_ref ? "&" : "")
 #define Z_REFCOUNT_PP(a) ((*a)->refcount)
 
 /* }}} */
@@ -42,87 +44,110 @@
 
 static int php_array_element_dump(zval **zv, int num_args, va_list args, zend_hash_key *hash_key)
 {
-	int level;
+	int level, output_html;
+	char *space, *newline;
 	TSRMLS_FETCH();
 
 	level = va_arg(args, int);
+	newline = va_arg (args, char *);
+	space = va_arg (args, char *);
+	output_html = va_arg (args, int);
 
 	if (hash_key->nKeyLength==0) { /* numeric key */
-		php_printf("%*c[%ld]=>\n", level + 1, ' ', hash_key->h);
+		php_printf("%*s[%ld]=>%s", level + 1, space, hash_key->h, newline);
 	} else { /* string key */
-		php_printf("%*c[\"%s\"]=>\n", level + 1, ' ', hash_key->arKey);
+		php_printf("%*s[\"%s\"]=>%s", level + 1, space, hash_key->arKey, newline);
 	}
-	php_var_dump(zv, level + 2 TSRMLS_CC);
+	php_var_dump(zv, level + 2 TSRMLS_CC, output_html);
 	return 0;
 }
 
-PHPAPI void php_var_dump(zval **struc, int level TSRMLS_DC)
+PHPAPI void php_var_dump(zval **struc, int level TSRMLS_DC, int output_html)
 {
 	HashTable *myht = NULL;
-	char *class_name;
+	char *class_name, newline[5], space[13], ampersand[6], *html_value;
 	zend_uint class_name_len;
 	zend_class_entry *ce;
+	int i, newlen;
+
+	if (output_html) {
+		strcpy (newline, "<br/>");
+		strcpy (space, "&nbsp;&nbsp;");
+		strcpy (ampersand, "&amp;");
+	}
+	else {
+		strcpy (newline, "\n");
+		strcpy (space, " ");
+		strcpy (ampersand, "&");
+	}
 
 	if (level > 1) {
-		php_printf("%*c", level - 1, ' ');
+		php_printf("%*s", level - 1, space);
 	}
 
 	switch (Z_TYPE_PP(struc)) {
 	case IS_BOOL:
-		php_printf("%sbool(%s)\n", COMMON, Z_LVAL_PP(struc)?"true":"false");
+		php_printf("%sbool(%s)%s", COMMON_HTML_SAFE, Z_LVAL_PP(struc)?"true":"false", newline);
 		break;
 	case IS_NULL:
-		php_printf("%sNULL\n", COMMON);
+		php_printf("%sNULL%s", COMMON_HTML_SAFE, newline);
 		break;
 	case IS_LONG:
-		php_printf("%sint(%ld)\n", COMMON, Z_LVAL_PP(struc));
+		php_printf("%sint(%ld)%s", COMMON_HTML_SAFE, Z_LVAL_PP(struc), newline);
 		break;
 	case IS_DOUBLE:
-		php_printf("%sfloat(%.*G)\n", COMMON, (int) EG(precision), Z_DVAL_PP(struc));
+		php_printf("%sfloat(%.*G)%s", COMMON_HTML_SAFE, (int) EG(precision), Z_DVAL_PP(struc), newline);
 		break;
 	case IS_STRING:
-		php_printf("%sstring(%d) \"", COMMON, Z_STRLEN_PP(struc));
-		PHPWRITE(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc));
-		PUTS("\"\n");
+		php_printf("%sstring(%d) \"", COMMON_HTML_SAFE, Z_STRLEN_PP(struc));
+		if (output_html) {
+			html_value = php_escape_html_entities (Z_STRVAL_PP(struc), Z_STRLEN_PP(struc), &newlen, 1, ENT_QUOTES, "");
+			PHPWRITE (html_value, newlen);
+			efree (html_value);
+		}
+		else {
+			PHPWRITE(Z_STRVAL_PP(struc), Z_STRLEN_PP(struc));
+		}
+		php_printf("\"%s", newline);
 		break;
 	case IS_ARRAY:
 		myht = Z_ARRVAL_PP(struc);
 		if (myht->nApplyCount > 1) {
-			PUTS("*RECURSION*\n");
+			php_printf("*RECURSION*%s", newline);
 			return;
 		}
-		php_printf("%sarray(%d) {\n", COMMON, zend_hash_num_elements(myht));
+		php_printf("%sarray(%d) {%s", COMMON_HTML_SAFE, zend_hash_num_elements(myht), newline);
 		goto head_done;
 	case IS_OBJECT:
 		myht = Z_OBJPROP_PP(struc);
 		if (myht && myht->nApplyCount > 1) {
-			PUTS("*RECURSION*\n");
+			php_printf("*RECURSION*%s", newline);
 			return;
 		}
 
 		ce = Z_OBJCE(**struc);
 
 		Z_OBJ_HANDLER(**struc, get_class_name)(*struc, &class_name, &class_name_len, 0 TSRMLS_CC);
-		php_printf("%sobject(%s)#%d (%d) {\n", COMMON, class_name, Z_OBJ_HANDLE_PP(struc), myht ? zend_hash_num_elements(myht) : 0);
+		php_printf("%sobject(%s)#%d (%d) {%s", COMMON_HTML_SAFE, class_name, Z_OBJ_HANDLE_PP(struc), myht ? zend_hash_num_elements(myht) : 0, newline);
 		efree(class_name);
 head_done:
 		if (myht) {
-			zend_hash_apply_with_arguments(myht, (apply_func_args_t) php_array_element_dump, 1, level);
+			zend_hash_apply_with_arguments(myht, (apply_func_args_t) php_array_element_dump, 4, level, newline, space, output_html);
 		}
 		if (level > 1) {
-			php_printf("%*c", level-1, ' ');
+			php_printf("%*s", level-1, space);
 		}
-		PUTS("}\n");
+		php_printf("}%s", newline);
 		break;
 	case IS_RESOURCE: {
 		char *type_name;
 
 		type_name = zend_rsrc_list_get_rsrc_type(Z_LVAL_PP(struc) TSRMLS_CC);
-		php_printf("%sresource(%ld) of type (%s)\n", COMMON, Z_LVAL_PP(struc), type_name ? type_name : "Unknown");
+		php_printf("%sresource(%ld) of type (%s)%s", COMMON_HTML_SAFE, Z_LVAL_PP(struc), type_name ? type_name : "Unknown", newline);
 		break;
 	}
 	default:
-		php_printf("%sUNKNOWN:0\n", COMMON);
+		php_printf("%sUNKNOWN:0%s", COMMON_HTML_SAFE, newline);
 		break;
 	}
 }
@@ -148,11 +173,35 @@
 	}
 	
 	for (i=0; i<argc; i++)
-		php_var_dump(args[i], 1 TSRMLS_CC);
+		php_var_dump(args[i], 1 TSRMLS_CC, 0);
 	
 	efree(args);
 }
 /* }}} */
+
+/* {{{ proto void var_dump_html(mixed var)
+   Dumps a string representation of variable to output */
+PHP_FUNCTION(var_dump_html)
+{
+        zval ***args;
+        int argc;
+        int     i;
+
+        argc = ZEND_NUM_ARGS();
+
+        args = (zval ***)emalloc(argc * sizeof(zval **));
+        if (ZEND_NUM_ARGS() == 0 || zend_get_parameters_array_ex(argc, args) == FAILURE) {
+                efree(args);
+                WRONG_PARAM_COUNT;
+        }
+
+        for (i=0; i<argc; i++)
+                php_var_dump(args[i], 1 TSRMLS_CC, 1);
+
+        efree(args);
+}
+/* }}} */
+
 
 /* {{{ debug_zval_dump */
 

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to