SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Howdy ho, everyone - just a quickie before I go to bed:

    How can I rip out line breaks and excessive spaces (i.e. more than one together) in a string?

    I.e. the spring must be:

    $string = Yes,yes, really good. yes yes yes good hhaha

    However, at the moment, the strings tend to be

    $string = Yes, yes, really good.
    yes yes yes good hhaha

    ...and that makes the javascript (that interprets the string) crazy, and crashes. Can you help me?

    /Mattias

  2. #2
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try ...
    str_replace(" ","",$string);
    str_replace("\n","",$string);
    etc

  3. #3
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Umm... wouldn't that replace ALL spaces? That would be really bad.

  4. #4
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    $string = trim($string);
    $string = str_replace("\n", " ", $string);


    That should trim all newlines from your string, as well as any whitespace at the start or end of the string.

  5. #5
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It would replace all double white spaces which is what I thought you wanted? I was just suggesting the use of str_replace() in general

    trim() is good as TWTC points out for leading and trailing space ,to remove a double newline you just str_replace("\n\n"... etc

  6. #6
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by firepages
    try ...
    str_replace(" ","",$string);
    str_replace("\n","",$string);
    etc

    firepages had
    Code:
    str_replace("  ","",$string);  // 2 spaces in the pattern
    So it should work w/ only double spaces. Might be not clear with non-fixed fonts
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  7. #7
    Non-Member
    Join Date
    Apr 2000
    Location
    Waco, Texas.
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    regexp,regexp,regexp!
    PHP Code:
    <?
    echo $foo eregi_replace("([ ]+)"," ","hello     how are     you?");
    //output: hello how are you?     
    ?>

  8. #8
    Wanna-be Apple nut silver trophy M. Johansson's Avatar
    Join Date
    Sep 2000
    Location
    Halmstad, Sweden
    Posts
    7,400
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Okidoki - got it working!

  9. #9
    imagine no limitations exbabylon's Avatar
    Join Date
    Dec 2000
    Location
    Idaho, USA
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how about

    PHP Code:
    function rip_clean($string){
    $srting trim($string);
    $srting str_replace("\r","",$string);
    $string str_replace("\n"," ",$string);
    return 
    $string;
    }

    $text "la la la la la la la la la la
    la la la la la la la la la la la la la
    la la la al 
    la la"
    ;

    $text rip_clean($text);
    echo 
    $text
    Blamestorming: Sitting around in a group discussing why a deadline was missed or a project failed and who was responsible.

    Exbabylon- Professional Internet Services


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •