Development PHP str_replace() and Arrays..

Discussion in 'Software' started by Sam0r, 8 Sep 2007.

  1. Sam0r

    Sam0r It's been a while

    Joined:
    24 Jun 2005
    Posts:
    1,900
    Likes Received:
    1
    I've got a problem where using two arrays with str_replace() just outputs the word 'Array' instead of the actual data inside the array.

    Here's the code:
    Code:
    <?php
    $input = ":) :O :D";
    $smiley_folder = "smileys/";
    
    $codes = array(':)', ':(', ':D', ':|', ':o', ':O', ':p', ':P', ';)');
    $images = array('smile.gif', 'sad.gif', 'biggrin.gif', 'surprise.gif', 'omg.gif', 'omg.gif', 'tongue.gif', 'tongue.gif', 'wink.gif');
    
    $output = str_replace($codes, "<img src=\"".$smiley_folder."".$images."\">", $input);
    echo $output;
    ?>
    
    The output is:
    Code:
    <img src="smileys/Array"> <img src="smileys/Array"> <img src="smileys/Array">
    
    The original idea was to use MySQL to store the smiley data, but I had the exact same problem, so decided to do it this way, which doesn't work either!

    Here's the culprit page: http://www.sam0r.co.uk/includes/smiley.php

    It might be something really simple, but I haven't coded PHP in a couple of years (Moved to ASP because of my job :eyebrow: )
     
  2. RTT

    RTT #parp

    Joined:
    12 Mar 2001
    Posts:
    14,120
    Likes Received:
    74
    $images is an array and you're trying to concatenate that array into a string as the replacement argument - that's why it won't work (and hence why you're seeing "Array").

    You either need to change the replacements to include the HTML or change the structure of the code you have.
     
  3. Sam0r

    Sam0r It's been a while

    Joined:
    24 Jun 2005
    Posts:
    1,900
    Likes Received:
    1
    Ah right! Yeah, that makes sense, thanks I'll give it a go :)

    Edit: Yep, that worked. Thanks RTT!
     
    Last edited: 8 Sep 2007

Share This Page