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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:10:29+00:00 2026-05-12T20:10:29+00:00

preg_match() gives one match. preg_match_all() returns all matches. preg_split() returns all splits. How can

  • 0

preg_match() gives one match.
preg_match_all() returns all matches.
preg_split() returns all splits.

How can I split only on the first match?

Example:

preg_split("~\n~", $string);

This is what I want:

array(
  0 => 'first piece',
  1 => 'the rest of the string
        without any further splits'
)
  • 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-12T20:10:29+00:00Added an answer on May 12, 2026 at 8:10 pm

    Simply set $limit to 2 for 2 parts of the array. Thanks to @BenJames for mentioning:

    preg_split("~\n~", $string, 2);
    

    I tested and it works fine.

    The limit argument:

    If specified, then only substrings up to limit are returned with the rest of the string being placed in the last substring. A limit of -1, 0 or null means "no limit" and, as is standard across PHP, you can use null to skip to the flags parameter.

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

Sidebar

Related Questions

We can easily check for a match in a string if (preg_match(/happy/i, happy is
In PHP one can use the function preg_match with the flag PREG_OFFSET_CAPTURE in order
preg_match('/te**ed/i', 'tested', $matches); gives me the following error: ERROR: nothing to repeat at offset
How can eregi() function can be replaced here with preg_match() function in below given
Possible Duplicate: preg_match() Unknown modifier '[' help I am trying to match this pattern
i'm using preg_match_all('/<?(.*)?>/', $bigString, $matches, PREG_OFFSET_CAPTURE); to find the contents of everything between <?
I'm using this line to match only certain browsers: RewriteCond %{HTTP_USER_AGENT} !((Chrome|Firefox|Safari|Opera)\/\d+(\.\d+)*|(MSIE|Opera) \d+(\.\d+)*|Maxthon) But
My form gives me a success message when all required fields are filled and
<? function recursiveSplit($string, $layer) { $err = preg_match_all("/\{(([^{}]*|(?R))*)\}/",$string,$matches); echo "Elementi trovati: $err<br>"; if($err ==
can some one please give me advice on how I make this block of

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.