Jump to content

preg_replace on indexed array


syn3rgy

Recommended Posts

I cant seem to get preg_replace to work on an array that contains two strings. var_dump of the array $catarray looks like this:

 


array(2) { [0]=> string(407) "category1\ncategory2\ncategory3\ncategory4" [1]=> string(1026) "category1\ncategory2\ncategory3\ncategory4\ncategory5" }

 

I am trying to eliminate the line breaks "\n" and replace them with ",". The code I wrote to try this is:

 


$test = preg_replace("(\n)",",",$catarray);
var_dump($test);

 

The dump looks exactly the same as the before I ran the preg_replace. If I run the code on a simple string like "category1\ncategory2\ncategory3\ncategory4" it works as expected. From what I understand about preg_replace it works on arrays, what am I missing?

 

Again any help would be great, I have exhausted my Google skills on this one.

 

Thanks for the time,

Benjamin

Link to comment
Share on other sites

  • 2 weeks later...

Note the way I have written the array and then looped through it performing the change

 

<?php

$array = array(
   "category1\ncategory2\ncategory3\ncategory4",
   "category1\ncategory2\ncategory3\ncategory4\ncategory5"
   );


foreach($array as $values){

   $newArray[] = preg_replace("(\n)",",",$values);   
}

echo "<pre>";
print_r($newArray);
echo "</pre>";

?>

Edited by is_numeric
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...