Jump to content

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


Z33M@N

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); 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.