making print_r use PHP_EOL

前端 未结 5 882
逝去的感伤
逝去的感伤 2021-01-13 23:42

My PHP_EOL is \"\\r\\n\", however, when I do print_r on an array each new line has a \"\\n\" - not a \"\\r\\n\" - placed after it.

Any idea if it\'s pos

5条回答
  •  情书的邮戳
    2021-01-14 00:29

    If you look the source code of print_r you'll find:

    PHP_FUNCTION(print_r)
    {
        zval *var;
        zend_bool do_return = 0;
    
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|b", &var, &do_return) == FAILURE) {
            RETURN_FALSE;
        }
    
        if (do_return) {
            php_output_start_default(TSRMLS_C);
        }
    
        zend_print_zval_r(var, 0 TSRMLS_CC);
    
        if (do_return) {
            php_output_get_contents(return_value TSRMLS_CC);
            php_output_discard(TSRMLS_C);
        } else {
            RETURN_TRUE;
        }
    }
    

    ultimately you can ignore the stuff arround zend_print_zval_r(var, 0 TSRMLS_CC); for your question.

    If you follow the stacktrace, you'll find:

    ZEND_API void zend_print_zval_r(zval *expr, int indent TSRMLS_DC) /* {{{ */
    {
        zend_print_zval_r_ex(zend_write, expr, indent TSRMLS_CC);
    }
    

    which leads to

    ZEND_API void zend_print_zval_r_ex(zend_write_func_t write_func, zval *expr, int indent TSRMLS_DC) /* {{{ */
    {
        switch (Z_TYPE_P(expr)) {
            case IS_ARRAY:
                ZEND_PUTS_EX("Array\n");
                if (++Z_ARRVAL_P(expr)->nApplyCount>1) {
                    ZEND_PUTS_EX(" *RECURSION*");
                    Z_ARRVAL_P(expr)->nApplyCount--;
                    return;
                }
                print_hash(write_func, Z_ARRVAL_P(expr), indent, 0 TSRMLS_CC);
                Z_ARRVAL_P(expr)->nApplyCount--;
                break;
    

    From this point on, you could continue to find the relevant line - but since there is already a hardcoded "Array\n" - i'll assume the rest of the print_r implementation uses the same hardcoded \n linebreak-thing.

    So, to answer your question: You cannot change it to use \r\n. Use one of the provided workarounds.

    Sidenode: Since print_r is mainly used for debugging, this will do the job as well:

    echo "
    ";
    print_r($object);
    echo "
    ";

提交回复
热议问题