Jump to content

Need Help! Trying to replace the word |LEFT| with Array("1","2","3");


Recommended Posts

Hi guys

 

I'm struggling a bit, I need to replace a word that occurs multiple times in text with an array("up","down","forward","backwards") of words.

 

$find = "left";
$replace = array("up","down","forward","backwards");
$text = "left left left left";

echo str_replace($find,$replace,$text);

 

The Output is: array array array array

 

Did try this with a foreach statement as well, but no luck. Is there a better way of doing this?  :confused:

 

Thanks

instead of echo str_replace($find,$replace,$text);

try below

$new_arr = str_replace($find,$replace,$text);

print_r($new_arr);

 

Hi thanks,

 

I tried it but still outputs: array array array array

 

The result I'm looking for is: up down forward backwards

 

How can I achieve that?

I'm not sure this will work, using str_replace to put an array in a string.

 

unless you want the results to be

"up down forwards backwards up down forwards backwards up down forwards backwards" etc..

 

then you would probably do

str_replace($find,implode(" ",$array),$text); 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.