Replace spaces in a string with '+'

Discussion in 'PHP' started by RogerDodgr, Dec 18, 2009.

  1. #1
    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):
     
    RogerDodgr, Dec 18, 2009 IP
  2. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #2
    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
     
    MyVodaFone, Dec 18, 2009 IP
  3. unitechy

    unitechy Peon

    Messages:
    350
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    foreach ($args as $arg){
    $result = preg_replace('/\s/', '+', $arg);
    }
     
    unitechy, Dec 18, 2009 IP
  4. astkboy2008

    astkboy2008 Peon

    Messages:
    211
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    try after the loop
    make str replace
     
    astkboy2008, Dec 18, 2009 IP
  5. creativeGenius

    creativeGenius Well-Known Member

    Messages:
    273
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    120
    #5
    basically...

    foreach($yourArray as $array)
    {
    $yourString = str_replace(" ","+", $array['string']
    }
     
    creativeGenius, Dec 18, 2009 IP
  6. ghprod

    ghprod Active Member

    Messages:
    1,010
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    78
    #6
    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
     
    ghprod, Dec 18, 2009 IP
  7. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #7
    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.
     
    joebert, Dec 19, 2009 IP
  8. RogerDodgr

    RogerDodgr Well-Known Member

    Messages:
    267
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #8
    Nice tip joebert. I guess it's obvious what sort of thing I'm working on.
     
    RogerDodgr, Dec 19, 2009 IP
  9. masterofweb

    masterofweb Greenhorn

    Messages:
    46
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #9
    echo str_replace(" ","+","Hello in this world");
     
    masterofweb, Dec 21, 2009 IP
  10. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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:
     
    JAY6390, Dec 21, 2009 IP
  11. RogerDodgr

    RogerDodgr Well-Known Member

    Messages:
    267
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    105
    #11
    Thanks Jay, I'm still getting used to php syntax and the foreach loop.
     
    RogerDodgr, Dec 21, 2009 IP
  12. CodedCaffeine

    CodedCaffeine Peon

    Messages:
    130
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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:
     
    CodedCaffeine, Dec 21, 2009 IP