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

Link to comment
Share on other sites

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.