Wierd str_replace problem?

Discussion in 'PHP' started by JosS, Aug 7, 2007.

  1. #1
    I'm grabbing results from a database, and in the names table alot of the results are like this

    'ONE'
    ' TWO '

    I'm trying to remove all the space around the name, so just the name is displayed.

    I use this

    str_replace(' ','',$row['name']);

    But that doesn't seem to work? I'm presuming that just removes one of the whitespaces and not them all. Do I need to create a loop, and if so how?

    Thanks in advance
     
    JosS, Aug 7, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Where are the other spaces visible? In the output (source code) that the script generates, or your web browser? Note that new line characters are displayed as space too. And if you want to remove the spaces around a text, you should have a look at trim().
     
    nico_swd, Aug 7, 2007 IP
  3. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #3
    The whitespace was probably tabs or newlines, not spaces. trim() takes care of all whitespace.
    trim()
    PHP:
    wtf nico :p (joking)
     
    krt, Aug 7, 2007 IP
  4. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Yeah, that's what its about. It's whitespace...

    Not fully understanding this trim section on the manual though..
     
    JosS, Aug 7, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    
    $row['name'] = trim($row['name']);
    
    PHP:
     
    nico_swd, Aug 7, 2007 IP
  6. JosS

    JosS Guest

    Messages:
    369
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks for that nico, I enjoy waiting for your responses in the PHP forum, you're always here to help!
     
    JosS, Aug 7, 2007 IP