xProteuSx Posted October 27, 2010 Share Posted October 27, 2010 HELP!!! PLEASE!!! Here's what's happening: I have a string much like this one: $str = "84,390961CPK_100,'Pink Coloured Cord',15,'390961CPK_100',1"; I need to insert apostrophes around the first instance of 390961CPK_100 so it reads '390961CPK_100' and not simply 390961CPK_100. To do this I have made the string into an array as follows: $str_array = str_split($str); Now I need to insert quotes into the array of characters, then convert the whole mess into back into a string. I can't go by hard coded index numbers, because all of the text in this string is variable in length, so I have to go by the first and second positions of the commas. I have no idea how to do this, and it needs to be done yesterday! Mucho Gracias to anyone who can help me with this. Link to comment https://forums.phpfreaks.com/topic/217049-insert-into-middle-of-array/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 27, 2010 Share Posted October 27, 2010 You would use explode to break the string into an array at the commas. Then you can easily alter just the second element. Then use implode to put the string back together. Link to comment https://forums.phpfreaks.com/topic/217049-insert-into-middle-of-array/#findComment-1127292 Share on other sites More sharing options...
mikosiko Posted October 27, 2010 Share Posted October 27, 2010 other alternative could be use str_replace() $str2 = str_replace(",390961CPK_100",",'39091CPK_100'", $str); Link to comment https://forums.phpfreaks.com/topic/217049-insert-into-middle-of-array/#findComment-1127296 Share on other sites More sharing options...
xProteuSx Posted October 27, 2010 Author Share Posted October 27, 2010 Thank you so much guys! I totally forgot about explode and implode (no, I don't work with arrays very often). If anyone is interested, here is what I came up with: $str = "84,390961CPK_100,'Pink Coloured Cord',15,'390961CPK_100',1"; $elements = explode(",", $str); $elements[1] = "'" . $elements[1] . "'"; $str = implode(",", $elements); echo $str; The output would result in: 84,'390961CPK_100','Pink Coloured Cord',15,'390961CPK_100',1 Now I don't have to pull the rest of my hair out! Thanks soooooooo much! Link to comment https://forums.phpfreaks.com/topic/217049-insert-into-middle-of-array/#findComment-1127306 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.