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 7836103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:58:20+00:00 2026-06-02T13:58:20+00:00

This question is very similar to use preg_split instead of split but I’ve got

  • 0

This question is very similar to use preg_split instead of split but I’ve got some confusions with the regex that I’d live to clear up.

Trying to update some existing split() functions to use preg_split() instead and I’m getting some unclear results. Running the code below will give me arrays of different lengths and I’m not sure why.

From what I can see split is matching on \n with a possible \r beforehand.
And I think preg_split is doing the same but then why is it creating 2 splits? Is this to do with lazy/greedy matching?

Demo code :

$test = "\r\n";

$val = split('\r?\n', $test); //literal interpretation of string
$val_new = split("\r?\n", $test); //php understanding that these are EOL chars
$val2 = preg_split('/\r?\n/', $test);

var_dump($val); // returns array(1) { [0]=> string(2) " " }
var_dump($val2); // returns array(2) { [0]=> string(0) "" [1]=> string(0) "" }

Edit : added in $val_new based on Kolinks comments because they helped clear up my understanding of the problem so may be of use to another too

  • 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-06-02T13:58:25+00:00Added an answer on June 2, 2026 at 1:58 pm

    split does not understand \r and \n as special characters, and because you used single quotes PHP doesn’t treat them as special characters either. So split is looking for literal \\n or \r\n.

    preg_split, on the other hand, does understand \r and \n as special characters, so even though PHP doesn’t treat them as such PCRE does and the string is therefore split correctly.

    This has nothing to do with lazy/greedy matching, it’s all because of the single quotes not parsing \r\n into their newline meanings.

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

Sidebar

Related Questions

My question is very similar to this unanswered one, with some small differences that
I know that this question is very similar to the question posted here .
My question popped up a very similar question, this one . But the accepted
I know that this is a repeated question. I have found very similar questions
I am having a problem very similar to this question here: Can I use
Very similar to this question , I am trying to convert a project that
Firstly, I realise that this is a very similar question to this one: Which
this question is very similar to another post I basically want to use the
My question is very similar to this topic . I'm trying to use jquery,
This looks very similar to my previous question because it's some sort of follow

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.