str_replace help.

Discussion in 'PHP' started by baris22, Dec 26, 2007.

  1. #1
    Hello all,

    How can i replace the spaces with one space using str_replace?

    Forexample
    
    
    this   is   a    sentence.
    
    I want to make it like:
    
    this is a sentence.
    PHP:
    
    $to_replace = array("\r\n","|","\n","\\r\\n","\\n");
    $title = str_replace($to_replace, " ", $title);
    
    PHP:
     
    baris22, Dec 26, 2007 IP
    Sabbir likes this.
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $text = preg_replace('~[\r\n\s\t]+~', ' ', $text);
    
    PHP:
     
    nico_swd, Dec 26, 2007 IP
  3. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #3
    thank you very much.


     
    baris22, Dec 26, 2007 IP
  4. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    hello,


    What about if i want to do same thing on &. How will i do.

    Instead of &&&&&&&&& I want &

    Thanks
     
    baris22, Dec 26, 2007 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    
    $text = preg_replace('~&{2,}~', '&', $text);
    
    PHP:
     
    nico_swd, Dec 27, 2007 IP
  6. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Thank you very much.
     
    baris22, Dec 28, 2007 IP
  7. Sabbir

    Sabbir Banned

    Messages:
    210
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #7
    excellent post. important for me too.
     
    Sabbir, Dec 29, 2007 IP