Given a string, How do I change all spaces in that string to a plus sign (+)? psuedo code: i.e. IF (char == space) THEN char = '+' I need to cycle through an array and replace the spaces in a string with a plus sign (+). for($j=1; $j<=20; $j++){ ... ???... $arr[0][$j] ...???? //<--This array holds the strings. ... } Code (markup):
If its of any use, you can do it with implode $string = this is a string of text; $NewString = implode('+',$string); result will be this+is+a+string+of+text
yes, i think this will solve the problem ... or u can use second solution by unitechy, using preg_replace and space regex is /\s/ regards
If the string doesn't contain anything other than alphanumeric characters, hyphens, underscores, and spaces, then the urlencode function should work. Probably easiest to stick with str_replace, but urlencode is still nice to know about.
foreach($array as &$value) { $value = str_replace(" ","+", $value); } PHP: either do it like that or use the urlencode() function on $value instead. If using urlencode you could also just use the array_map on the whole array in one line $array = array_map('urlencode', $array); PHP:
For something involving an array loop to everything, I'd personally use this: /** * remove_item() * Removes an item from the input and returns it. */ function remove_item(&$input, $remove) { # Search through the $input for $remove, then replace it with nothing. $input = str_replace($remove, '', $input); } # Essentially walks through each array item and runs the function with a "space" parameter for # the $remove option in the function. array_walk($array, 'remove_item', ' '); PHP: