www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Server-Side Development > PHP

    PHP Discussion and technical support for using and deploying PHP based websites.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 08-09-2005, 11:52 PM
    cberg76 cberg76 is offline
    Registered User
     
    Join Date: Dec 2004
    Posts: 53
    remove newline characters from a string

    Hello,

    What is the easiest method to remove newline characters from a string?

    Basically, I'd like it to function like this:

    $newstring = rmNewline($oldstring);

    The newstring should now have no newline characters '\n'

    Thanks!
    Reply With Quote
      #2  
    Old 08-10-2005, 01:34 AM
    Stephen Philbin's Avatar
    Stephen Philbin Stephen Philbin is offline
    Thuper Moderator
     
    Join Date: Mar 2004
    Posts: 3,059
    PHP Code:
    $newstring = preg_replace("/[\n\r]/","",$subject);
    Should do it. Where $subject is the original string you're wanting to remove the newlines from.
    Reply With Quote
      #3  
    Old 08-10-2005, 02:51 AM
    cberg76 cberg76 is offline
    Registered User
     
    Join Date: Dec 2004
    Posts: 53
    Worked flawlessly! I appreciate your assistance. Thank you very much. I knew there must be some library method so I wouldn't have to reinvent the wheel. thanks for the extra help with the tricky syntax too that would have got me next. I recognize the 2nd and 3rd parameters but the 1st one has some tricky syntax. You da man!
    Reply With Quote
      #4  
    Old 08-10-2005, 06:56 AM
    BeachSide's Avatar
    BeachSide BeachSide is offline
    Yo Yo Yo
     
    Join Date: Jan 2005
    Location: Lithia Springs, GA USA
    Posts: 889
    or you could go the less overhead route (not using the reg_ex engine) and simply use trim...
    PHP Code:
    $string = "This is a string with a newline in it\n";

    $newString = trim($string);
    Not necessarily better than Mr H's method just a different way of doing it
    Reply With Quote
      #5  
    Old 08-10-2005, 09:37 AM
    NogDog's Avatar
    NogDog NogDog is offline
    High Energy Magic Dept.
     
    Join Date: Aug 2004
    Location: Ankh-Morpork
    Posts: 13,641
    Quote:
    Originally Posted by BeachSide
    or you could go the less overhead route (not using the reg_ex engine) and simply use trim...
    PHP Code:
    $string = "This is a string with a newline in it\n";

    $newString = trim($string);
    Not necessarily better than Mr H's method just a different way of doing it
    However, trim() would only take care of newlines at the very beginning or end of the string, but would not work for something like "This is a\nstring with two\nnewlines in the middle.", in which case I'd probably want to replace them with space characters:
    PHP Code:
    $newstring = str_replace("\n", " ", $oldstring);
    If you don't want the possibility of adding leading/trailing spaces to the string, then combine with trim():
    PHP Code:
    $newstring = str_replace("\n", " ", trim($oldstring));
    __________________
    "That's what the gods are! An answer that will do! Because there's food to be caught and babies to be born and life to be lived and so there is not time for big, complicated, and worrying answers! Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." -- from Nation, by Terry Pratchett
    freelancer.internet.com
    Email me
    Reply With Quote
      #6  
    Old 08-10-2005, 07:18 PM
    BeachSide's Avatar
    BeachSide BeachSide is offline
    Yo Yo Yo
     
    Join Date: Jan 2005
    Location: Lithia Springs, GA USA
    Posts: 889
    Quote:
    Originally Posted by NogDog
    However, trim() would only take care of newlines at the very beginning or end of the string, but would not work for something like "This is a\nstring with two\nnewlines in the middle.", in which case I'd probably want to replace them with space characters:
    PHP Code:
    $newstring = str_replace("\n", " ", $oldstring);
    If you don't want the possibility of adding leading/trailing spaces to the string, then combine with trim():
    PHP Code:
    $newstring = str_replace("\n", " ", trim($oldstring));
    AH HAH! See man that is why you are great! I didn't even think about that scenerio.
    Reply With Quote
      #7  
    Old 08-10-2005, 10:47 PM
    Stephen Philbin's Avatar
    Stephen Philbin Stephen Philbin is offline
    Thuper Moderator
     
    Join Date: Mar 2004
    Posts: 3,059
    Actually, you both had good points. I've been arseing about with preg_replace() so much lately I'd forgotten str_replace() and str_ireplace() even existed. Both of those are faster and less "cpu expensive" than preg_replace, but Zooper-Nog here also pointed out that there may also be newlines with no space characters on either side of it. Applying my response to such a case would cause the words of either side to get concatenated, which wouldn't be too helpful. To fix such a problem we would have to chuck str_replace() and str_ireplace out in favour of preg_reaplace, but I lack the know-how to complete the function. I'll write the function as much as I can, but you'll have to wait for someone more knowledgeable than I to complete it.

    Here goes:
    PHP Code:

    function kill_dem_durdy_noo_lynz($input)
    {
        
    $find = array(
        
    "[\n\r]((?![^ ])|(A non-capturing look behind for a space))",
        
    "[\n\r]((?! )(A non-capturing negative look behind for a space))"
        
    );
        
        
    $replace = array(
        
    "",
        
    " "
        
    );
        
        return
    preg_replace($find,$replace,$input);
    }
    I dunno how to do look behinds in regexp (non-capturing or otherwise), but if anyone can fill that gap, I think that that function should do all that is required.

    Oh yeah, ya might wanna rename the function to something slightly more paractical too.
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 09:46 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.