Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7051605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:17:00+00:00 2026-05-28T03:17:00+00:00

I need to split the following string and put each new line into a

  • 0

I need to split the following string and put each new line into a new array element.

this is line a.(EOL chars = '\r\n' or '\n')
(EOL chars)
this is line b.(EOL chars)
this is line c.(EOL chars)
this is the last line d.(OPTIONAL EOL chars)

(Note that the last line might not have any EOL characters present. The string also sometimes contains only 1 line, which is by definition the last one.)

The following rules must be followed:

  • Empty lines (like the second line) should be discarded and not put
    into the array.
  • EOL chars should not be included, because otherwise
    my string comparisons fail.

So this should result in the following array:

[0] => "this is line a."
[1] => "this is line b."
[2] => "this is line c."
[3] => "this is the last line d."

I tried doing the following:

$matches = array();
preg_match_all('/^(.*)$/m', $str, $matches);
return $matches[1];

$matches[1] indeed contains each new line, but:

  • Empty lines are included as well
  • It seems that a ‘\r’ character gets smuggled in anyway at the end of the strings in the array. I suspect this has something to do with the regex range ‘.’ which includes everything except ‘\n’.

Anyway, I’ve been playing around with ‘\R’ and whatnot, but I just can’t find a good regex pattern that follows the two rules I outlined above. Any help please?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:17:01+00:00Added an answer on May 28, 2026 at 3:17 am

    Just use preg_split() to split on the regular expression:

    // Split on \n, \r is optional..
    // The last element won't need an EOL.
    $array = preg_split("/\r?\n/", $string);
    

    Note, you might also want to trim($string) if there is a trailing newline, so you don’t end up with an extra empty array element.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to split my string input into an array at the commas. Is
In C# I need to split a string (a log4j log file) into array
Have the following string i need to split. $string = This is string sample
I have a string like this This:string:must~:be:split:when:previous:char:is:not~:this I need to split the line with
i have the following string: http://pastebin.com/d29ae565b i need to separate each value and put
I need to split a string into chunks of 2,2,3,3 characters and was able
I have a long string (multiple paragraphs) which I need to split into a
Looking for some help! I need to split a string at the last occurrence
I have the following string called MasterVersion : 1.1-SNAPSHOT I need to split it
Similar to this thread for C# , I need to split a string containing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.