Print Array nicely - str_replace error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Print Array nicely - str_replace error

Post 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;
Last edited by tecktalkcm0391 on Sat Jan 30, 2010 2:31 pm, edited 1 time in total.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: str_replace error

Post 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;
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: str_replace error

Post by tecktalkcm0391 »

Oh, okay. Then how do you do the same replacement but only one time.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: str_replace error

Post 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?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: str_replace error

Post 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().
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: str_replace error

Post 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; 
}
 
?>
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Print Array nicely - str_replace error

Post by AbraCadaver »

Ahh... Kind of like:

Code: Select all

echo "<pre>"; print_r($_POST); echo "</pre>";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Print Array nicely - str_replace error

Post 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));
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Print Array nicely - str_replace error

Post by tecktalkcm0391 »

Haha, thanks that's a lot easier :)
Post Reply