syn3rgy Posted March 17, 2011 Report Share Posted March 17, 2011 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 Quote Link to comment Share on other sites More sharing options...
is_numeric Posted March 30, 2011 Report Share Posted March 30, 2011 (edited) 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 March 30, 2011 by is_numeric Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.