Page 1 of 1

Print Array nicely - str_replace error

Posted: Sat Jan 30, 2010 1:08 pm
by tecktalkcm0391
I can't figure out why the following code keeps returning this error:
Fatal error: Only variables can be passed by reference in file.php on line 10

Code: Select all

 
    $post = print_r($_POST, true);
    $post = explode("\n", $post);
    $output = '';
    $add = '';
    $l = '<br>';
    $tab = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    foreach($post as $cur){
        if(stripos($cur, ')') !== false){
            $add = str_replace($tab, '', $add, 1);
        } 
        $output.= $add.$cur.$l;
        if(stripos($cur, '(') !== false){
            $add = $tab.''.$add;
        }
    }
    echo $output;

Re: str_replace error

Posted: Sat Jan 30, 2010 1:53 pm
by AbraCadaver
The fourth parameter is not a value that you pass. It needs to be a var that str_replace() modifies to hold the count of the replacements made.

Code: Select all

$add = str_replace($tab, '', $add, $count);
echo $count;

Re: str_replace error

Posted: Sat Jan 30, 2010 1:58 pm
by tecktalkcm0391
Oh, okay. Then how do you do the same replacement but only one time.

Re: str_replace error

Posted: Sat Jan 30, 2010 2:04 pm
by AbraCadaver
tecktalkcm0391 wrote:Oh, okay. Then how do you do the same replacement but only one time.
I'm curious about your code. What are you trying to do in general?

Re: str_replace error

Posted: Sat Jan 30, 2010 2:13 pm
by AbraCadaver
Sorry... To answer your question you could try preg_replace() which has a limit parameter that works the way you were trying to do it with str_replace().

Re: str_replace error

Posted: Sat Jan 30, 2010 2:25 pm
by tecktalkcm0391
Okay I got it now, below is the final code.

What I did was create a page to post data, so I can see what was being posted, but when you use print_r it prints out correctly spaced, but only if you look at the source code, because it has no HTML codes to space it correctly in the browser, so this will make it output so that I won't have to look at the source code, because it'll look nice in the browser. :P

Code: Select all

<?php 
 
if(empty($_POST)){
    echo "No post data found.";
} else {
    $post = print_r($_POST, true);
    $post = explode("\n", $post);
    
    $output = '';
    $add = '';
    $l = '<br>';
    $tab = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
    foreach($post as $cur){
        
        if($cur == "\n" || $cur == ''){
            continue;
        }
                
        if(stripos($cur, ')') !== false){
            $add = preg_replace("[(&nbsp;){6,6}]", '', $add, 1);
        } 
        
        $output.= $add.$cur.$l;
        
        if(stripos($cur, '(') !== false){
            $add = $tab.''.$add;
        }
        
        
    }
    
    echo $output; 
}
 
?>

Re: Print Array nicely - str_replace error

Posted: Sat Jan 30, 2010 3:50 pm
by AbraCadaver
Ahh... Kind of like:

Code: Select all

echo "<pre>"; print_r($_POST); echo "</pre>";

Re: Print Array nicely - str_replace error

Posted: Sat Jan 30, 2010 4:07 pm
by AbraCadaver
You could also try one of these:

Code: Select all

highlight_string(print_r($_POST, true));
//or
highlight_string(var_export($_POST, true));

Re: Print Array nicely - str_replace error

Posted: Sat Jan 30, 2010 6:13 pm
by tecktalkcm0391
Haha, thanks that's a lot easier :)