Jump to content

[SOLVED] str_replace in array


Recommended Posts

Hi, I have a little problem with searching and replacing slashes (/) with dashes (-) inside an array.

 

I tried the following code:

$replace = $array;
str_replace(
array('/'),
'-',
$replace
);

 

That did not work. I than thought maybe it helps if I convert the array first into a string than do the str_replace and after that convert the string back into an array again. I used for that the following code:

# Convert slash (/) into dash (-) in array #
// First convert array to string
$string = implode('<br />', $array_in);

// Search and replace in string
$replace = $string;
str_replace(
array('/'),
'-',
$replace
);

// convert string back to array
$array_out = explode(',', $replace);

 

When I now echo the $array_out it just gives me a long list with the text array which would look nice above my bed of course but is not really what I had in mind right now :)

 

Can some one point me out here?

 

All help is appreciated!

 

Kind regards,

Sebastiaan

Link to comment
https://forums.phpfreaks.com/topic/53961-solved-str_replace-in-array/
Share on other sites

Read the manual:

 

mixed str_replace ( mixed $search, mixed $replace, mixed $subject [, int &$count] )

 

If subject is an array, then the search and replace is performed with every entry of subject, and the return value is an array as well.

 

 

 

<?php

//$array is your array

$array = str_replace("/", "-", $array);

?>

 

 

Orio.

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.