Results 1 to 30 of 30

Thread: newlines?

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    newlines?

    Is there a function that trims trailing and leading newlines but NOT spaces?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  2. #2
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    PHP Code:
    $mystr=str_replace("\n","",$mystr); 
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by nabeels786
    PHP Code:
    $mystr=str_replace("\n","",$mystr); 
    I want to strip TRAILING and LEADING newlines in a string, not all of them in the string.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    I think if you add a second argument to the trim function it should work..

    $trimmedStr = trim($theOriginalStr, "\n");

  5. #5
    scoutt
    Guest
    $mystr=str_replace("\r\n","",$mystr);

    if that doesn't work give us an example of wht you are after.

  6. #6

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    Re: newlines?

    Originally posted by The Hobo
    Is there a function that trims trailing and leading newlines but NOT spaces?
    I don't know if I can be more explainitory than this. I have a string that I want to strip LEADING and TRAILING newline characters, but NOT leading and trailing spaces.

    Trim removes BOTH, and str_replace will remove all occurances of them in the string.

    I hope this helps clear things up. TheGoldenShogun I don't think trim has a second parameter:

    string trim (string str)

    This function strips whitespace from the start and the end of a string and returns the stripped string. The whitespace characters it currently strips are: "\n", "\r", "\t", "\v", "\0", and a plain space.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    If there's no function to do this, I'd probably have to write one:

    PHP Code:
    function stripem($string) {
      
    $x strlen($string);
      for (
    $i 0$i $x$i++) {

        
    //cycle through each character
        //if the character is a new line
        //start the string after the current $i

        //if it's not, break and exit loop

      
    }
      
    //do the same thing for the end of the string, somehow


    I have the basic idea, I just need to fill in the blanks.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    Yeah the loop is probably the best way to go... I'm pretty sure it takes a second argument though

    string trim ( string str [, string charlist])


    Note: The optional charlist parameter was added in PHP 4.1.0

    This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters: Without the second parameter, trim() will strip these characters:

    " " (ASCII 32 (0x20)), an ordinary space.

    "\t" (ASCII 9 (0x09)), a tab.

    "\n" (ASCII 10 (0x0A)), a new line (line feed).

    "\r" (ASCII 13 (0x0D)), a carriage return.

    "\0" (ASCII 0 (0x00)), the NUL-byte.

    "\x0B" (ASCII 11 (0x0B)), a vertical tab.

    ex.
    $trimmed = trim($text," \t.");

    all this comes from php.net
    http://www.php.net/manual/en/function.trim.php

  9. #9

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Oh, I looked here:

    http://www.phpbuilder.com/manual/function.trim.php

    They must not be up to date then. I'll try it when I get home from school.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  10. #10
    scoutt
    Guest
    what is it that you are trying to do Hobo?

  11. #11

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by scoutt
    what is it that you are trying to do Hobo?
    As, I said. I don't know how to make myself clearer. Perhaps I have a problem with expressing myself but maybe an example would help:

    My String (lets say $string): "\n\n The\nDog \n\n" I want to strip newline characters (\n), but now spaces ( ). So that my string would now be: " The\nDog "

    str_replace would replace all newlines (I only want to replace the trailing and leading ones) and trim would replace the spaces before The and after Dog, which I want to preserve.

    But I will give what TheGoldenShogun just pointed out a shot and see if it works (which I'm sure it will)
    My evil laugh has a squeak in it.

    kristopherwilson.com

  12. #12

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by TheGoldenShogun
    Yeah the loop is probably the best way to go... I'm pretty sure it takes a second argument though

    string trim ( string str [, string charlist])
    Indeed. I guess I can't be so reliable on a secondary source. Thanks alot, GoldenShogun.
    Last edited by The Hobo; Oct 4th, 2002 at 05:01 PM.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  13. #13
    scoutt
    Guest
    ok that makes sence. so basically you have this

    "\n\n The\nDog \n\n" and you want it to look like this

    "The\nDog"

    so basically it would look like this

    The
    Dog

    so I don't see the point in it, but not my concern. so if you have 2 \n\n and you know they are there why don't you do this

    $mystr=str_replace("\n\n","",$mystr);

    how are these being saved? are they coming from a database or they a text file.

  14. #14
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    Yeah if you want all the info on something, go to the source, PHP.net is their official website... real easy to use, great search engine.

  15. #15

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by scoutt
    ok that makes sence. so basically you have this

    "\n\n The\nDog \n\n" and you want it to look like this

    "The\nDog"

    so basically it would look like this

    The
    Dog
    scoutt, I'm not going to waste anymore time trying to explain this to you. Read all the words in my post, don't just assume things. The keyword is SPACE. I don't want to remove the space.

    Notice the difference between:

    "The\nDog" and " The\nDog "

    I know you're just trying to help me and everything, but if you're not going to make an attempt at reading my posts, don't try to help me. I don't need to explain myself every other post.

    And furthermore, again I say, "$mystr=str_replace("\n\n","",$mystr);" Will replace all occurances of "\n" in $mystring. So that newline betwen The and Dog will be removed, and I don't want that.

    Do you understand me now? Am I getting through to you?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  16. #16
    scoutt
    Guest
    pull your head out of your ass for just one moment. I asked those question for a reason. if you are adding it to a database then you might get away with some other code so you don't have to do what you are trying to do.

    also this

    $mystr=str_replace("\n\n","",$mystr);"

    look real closely the \n\n is not \n. so you will be taking the \n\n out instead of \n. also you can add a space in there like so


    $mystr=str_replace("\n\n"," ",$mystr);"

    notice the space.??? it will replace all \n\n with a SPACE. dumb****but af course I don't know what I'm talking about......

  17. #17

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    That's wonderful. Now what if I have "The\n\nDog?" It get's taken out it then too doesn't? I have no idea what the user is going to enter, but I have to preserve the spaces. So it will replace all occurances of '\n\n' in my string. Same problem.

    Way to be such an overdramatic idiot though. It really works out great for you.

    Now here's a spoon...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  18. #18
    scoutt
    Guest
    now if you would read my post I said if you know for a fact that there is only \n\n in it and the middle one is \n it would work, but since you don't know then that won't do the job. hmmmm if this is the case then your loop will not work either.

    it sounds like an input field how are they going to enter a return in that. if a textarea then I wouldn't worry about it. I enter code in my textareas and I don't worry about it. but hey if you want the extra work then I won't help you.

    but since you are too lazy then i will not tell you to use

    ltrim - Strip whitespace from the beginning of a string

    and

    rtrim - Strip whitespace from the end of a string

    later.

  19. #19

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by scoutt
    now if you would read my post I said if you know for a fact that there is only \n\n in it and the middle one is \n it would work, but since you don't know then that won't do the job. hmmmm if this is the case then your loop will not work either.

    it sounds like an input field how are they going to enter a return in that. if a textarea then I wouldn't worry about it. I enter code in my textareas and I don't worry about it. but hey if you want the extra work then I won't help you.

    but since you are too lazy then i will not tell you to use

    ltrim - Strip whitespace from the beginning of a string

    and

    rtrim - Strip whitespace from the end of a string

    later.
    The loop thing would to have worked fine. What I was suggesting was that to go character by character and when the character ISN'T a new line, break from the loop.

    I already got a working answer to this anyways. And I know of the ltrim and rtrim functions. they won't help me either. They kill spaces.

    And since you don't know what my problem is, why are you suggesting that I don't worry about it and that it's going to be extra work for me? Don't second guess what I'm doing.

    The user puts in input, it gets saved in a database, and later is formatted into color coded html to look like VB code does in the IDE. However, through this process an extra newline or possibly two is added to the end and beginings of the text. I simply wanted to strip them.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  20. #20
    scoutt
    Guest
    yup you are right, I am stupid. I use the same process and I don't have newlines at the beginning.

    and your right about the loop if is doesn't detect a newline it would break out of the loop. of course this is not a newline "\n"

    so, off with me...

  21. #21

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    oh, a \n isn't a new line?

    "\n" (ASCII 10 (0x0A)), a new line (line feed).

    from php.net
    Maybe not in your own personal terminology.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  22. #22
    scoutt
    Guest
    "\n" (ASCII 10 (0x0A)), a new line (line feed).

    from php.net

    hmmmmm wonder why it says new line.

  23. #23

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    what the hell are you talking about, dude? You make no sense...You're the one that said it wasn't a new line.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  24. #24
    scoutt
    Guest
    The loop thing would to have worked fine. What I was suggesting was that to go character by character and when the character ISN'T a new line, break from the loop.
    trying to tell you that your loop will not work if you check for newlines. the isn't is the key word.

    if you loop through this to detect \n (newline) it will take out all of them
    "\n\n The\nDog \n\n"

    same with this

    "\n\n The\n\nDog \n\n"

    your loop will do the same as this

    $mystr=str_replace("\n"," ",$mystr);

    so tell me the way you fixed it if you don't mind.

  25. #25

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Originally posted by scoutt


    trying to tell you that your loop will not work if you check for newlines. the isn't is the key word.

    if you loop through this to detect \n (newline) it will take out all of them
    "\n\n The\nDog \n\n"

    same with this

    "\n\n The\n\nDog \n\n"

    your loop will do the same as this

    $mystr=str_replace("\n"," ",$mystr);

    so tell me the way you fixed it if you don't mind.
    Okay, obviously we aren't understanding each other at all.

    I was saying that I would loop through the text and trim it down each time until the current character is not a new line.

    Lets say we have "\n\n The\n\nDog \n\n"

    It would go character by character (assuming that a new line is counted as one character):

    Is \n a new line? Yes, trim the string to start at 1.
    Is \n a new line? Yes, trim the string to start at 1.
    Is a new line? No, it's a space, break from the loop.
    I guess I'll write up the example to show you what I mean. I fixed it by using Trim with the second parameter as stated by TheGoldenShogun.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  26. #26
    scoutt
    Guest
    hehe you are funny.

    what did you plan on using in your loop?

    str_replace or trim.

    either one would do fine without the loop. if you didn't use any of those then you would never reach the end of the string. you would always stop at " The" . if the character is not a new line then exit. that is all I was saying.

  27. #27

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Okay, it's sloppy, but this is what I meant:

    PHP Code:
    <?php

       $ourstring 
    "\n\n The\n\n Dog \n\n";
       
    $x strlen($ourstring);
       echo 
    'Before:<pre style="background-color: silver;">' $ourstring .'</pre>';
       for (
    $i 0$i $x$i++) {
            if (
    substr($ourstring01) == "\n") {
                
    $ourstring substr($ourstring1);
            } else {
                break;
            }            
       }
       
    $x strlen($ourstring);
       for (
    $i 0$i $x$i++) {
            if (
    substr($ourstring$x 11) == "\n") {
                
    $ourstring substr($ourstring0$x 1);
                
    $x strlen($ourstring);
            } else {
                break;
            }
       }
       echo 
    'After:<pre style="background-color: silver;">' $ourstring .'</pre>';

    ?>
    The result is here: http://www.vbshelf.com/test2.php

    It scans through the begining of the string, character by character and trimming it down until it comes across a character that is not a new line. Then it does the same thing at the end.

    But the same thing can be accomplished with Trim($ourstring, "\n")
    My evil laugh has a squeak in it.

    kristopherwilson.com

  28. #28
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    Geez, listen to you two go back and forth. We got a little cat fight going on in the forum. And yesterday was the 10 year anniversary of Rodney King's "Cant we all just get along?" speech... he he, it's always fun to have a simple post turn into a 27-28 message arguement every now and then. Hey, we're programmers, we gotta relieve stress on one another every now and then...


  29. #29

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    It probably would have only been a few bickering posts, but I can never understand what he's talking about...
    My evil laugh has a squeak in it.

    kristopherwilson.com

  30. #30

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Ah, the good old days
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

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



Click Here to Expand Forum to Full Width