SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replacing multiple cr/lf with singles

    I have a form on my website where people can enter text as they please. I later want to format the text into a reviews page that has consistent formatting. Some people like to single space their paragraphs and others like to put in multiple lines. I'm trying to write a script that can strip more than one occurrence of new lines/ carriage returns so that all text is displayed with a single cr between paragraphs. I suppose I need to be cognizant of text submitted on Unix vs. PC vs. Mac?

    Does anyone have any advice for doing this? Is this a job for RegEx? I hope not because I am complete RegTard.

    Thanks!

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, try
    PHP Code:
    preg_replace("~(\r|\n|\r\n)+~""\r\n"$text); 

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect. Works like a charm. Thanks.


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
  •