sebastiaandraaisma Posted June 2, 2007 Share Posted June 2, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/53961-solved-str_replace-in-array/ Share on other sites More sharing options...
chigley Posted June 2, 2007 Share Posted June 2, 2007 <?php $array = array("-", "a", "/", "b"); foreach($array as $key => $value) { $value = str_replace("/", "-", $value); $array[$key] = $value; } // array("/", "a", "/", "b"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/53961-solved-str_replace-in-array/#findComment-266754 Share on other sites More sharing options...
Orio Posted June 2, 2007 Share Posted June 2, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/53961-solved-str_replace-in-array/#findComment-266756 Share on other sites More sharing options...
sebastiaandraaisma Posted June 2, 2007 Author Share Posted June 2, 2007 Thank you both for such a quick reply! The solution Orio provided worked the best for me Thanks again! Kind regards, Sebas Quote Link to comment https://forums.phpfreaks.com/topic/53961-solved-str_replace-in-array/#findComment-266764 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.