gazever Posted February 22, 2007 Share Posted February 22, 2007 Hi I was given this code yesterday by effigy, <pre> <?php $data = '8,6,9,2,,,7,4,5,3,,,2,1,5,3,4,,,6,9,4,5,3,6,9,8'; $data = str_replace(',,', ', ,', $data); $array = explode(',', $data); print_r($array); ?> </pre> However this string opens from a txt file and replaces the ,, with a space character, I loop through this a few times, incase there are two empty values in a row, producing ,,, However how would I loop thorugh the array and replace all values that are "" empty to the value, as if the first value of the array is empty It will not change to a space? I'm sure this is really simple to do. Thanks Gaz Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted February 22, 2007 Share Posted February 22, 2007 if( $data{0} == ',' ) { $data = '$nbsp;'.$data; print $data; } Try this. Check character 0, if it is a ',', insert a monk.e.boy Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted February 22, 2007 Share Posted February 22, 2007 not $nbsp; monk.e.boy. Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted February 22, 2007 Share Posted February 22, 2007 not $nbsp; monk.e.boy. Oh yeah. monk.e.boy Quote Link to comment Share on other sites More sharing options...
gazever Posted February 22, 2007 Author Share Posted February 22, 2007 thanks, sent me in the right direction, I used this within my existing loop if ($array[$i] == "") { $array[$i] = " "; } Gaz Quote Link to comment Share on other sites More sharing options...
monk.e.boy Posted February 23, 2007 Share Posted February 23, 2007 thanks, sent me in the right direction, I used this within my existing loop if ($array[$i] == "") { $array[$i] = " "; } Gaz if ( strlen( $array[$i] ) == 0) { $array[$i] = " "; } would be my prefered solution ;D ;D ;D (joke) monk.e.boy 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.