cyberkiller Posted May 8, 2007 Share Posted May 8, 2007 So I have an array with 10 items in it. How do I go about clearing all spaces before the data? Ex: Before: jacob After: jacob Also some of the data looks like this, World of Warcraft Guide How do I go about checking each value for this   and replacing it with a space if its there? Thanks Link to comment https://forums.phpfreaks.com/topic/50534-cleaning-up-data-in-an-array/ Share on other sites More sharing options...
per1os Posted May 8, 2007 Share Posted May 8, 2007 <?php $string = "World of Warcraft Guide"; $string = str_replace(' ', ' ', $string); <?php $string = ' jacob'; $string = trim($string, ' '); www.php.net/trim www.php.net/str_replace Link to comment https://forums.phpfreaks.com/topic/50534-cleaning-up-data-in-an-array/#findComment-248304 Share on other sites More sharing options...
Barand Posted May 8, 2007 Share Posted May 8, 2007 Putting it all together <?php $ar = array ( ' jacob', 'Esau', 'World of Warcraft Guide' ); foreach ($ar as $k => $val) { $ar[$k] = trim(str_replace(' ', ' ', $val)); } /** * check results */ echo '<pre>', print_r($ar, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/50534-cleaning-up-data-in-an-array/#findComment-248407 Share on other sites More sharing options...
cyberkiller Posted May 9, 2007 Author Share Posted May 9, 2007 Doesn't seem to work for me, here is what I used foreach ($parsed as $k => $val) { $parsed[0][$k] = trim(str_replace(' ', ' ', $val)); } No errors, just that the values still have the spaces and   Where the 10 values are in, $parsed[0][0], $parsed[0][1], etc.. Link to comment https://forums.phpfreaks.com/topic/50534-cleaning-up-data-in-an-array/#findComment-248774 Share on other sites More sharing options...
boo_lolly Posted May 9, 2007 Share Posted May 9, 2007 if you would have used the code barand gave you, it would have worked. Link to comment https://forums.phpfreaks.com/topic/50534-cleaning-up-data-in-an-array/#findComment-248793 Share on other sites More sharing options...
cyberkiller Posted May 9, 2007 Author Share Posted May 9, 2007 I did start out with barand's code, however I don't think he knew I was using a multi array (whatever their called), after playing with it, foreach ($parsed[0] as $k => $val) { $parsed[0][$k] = trim(str_replace(' ', ' ', $val)); } Seems to work, it wasn't barand's fault he just didnt know how my array was setup. Link to comment https://forums.phpfreaks.com/topic/50534-cleaning-up-data-in-an-array/#findComment-248799 Share on other sites More sharing options...
Barand Posted May 9, 2007 Share Posted May 9, 2007 Telepathy repairman calling next Friday Link to comment https://forums.phpfreaks.com/topic/50534-cleaning-up-data-in-an-array/#findComment-249217 Share on other sites More sharing options...
boo_lolly Posted May 10, 2007 Share Posted May 10, 2007 I did start out with barand's code, however I don't think he knew I was using a multi array (whatever their called)... you could have created a recursive function: <?php function stripString($array){ foreach($array as $key => $val){ if(is_array($val)){ stripString($val); } $array[$key] = trim(str_replace(' ', ' ', $val)); } } $ar = array ( ' jacob', 'Esau', 'World of Warcraft Guide' ); stripString($ar); ?> Link to comment https://forums.phpfreaks.com/topic/50534-cleaning-up-data-in-an-array/#findComment-249426 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.