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

  • Home
  • SEARCH
  • 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 8763749
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:51:20+00:00 2026-06-13T15:51:20+00:00

I am trying to learn regex in PHP and messing around with the preg_split

  • 0

I am trying to learn regex in PHP and messing around with the preg_split function.

It doesn’t appear to be correct though, or my understanding is completely wrong.

The test code i am using is:

$string = "test ing ";
var_dump(preg_split('/t/', $string));

I would expect to get an array like the following:

[0] => "es" [1] => " ing "

but the following is being returned:

[0] => "" [1] => "es" [2] => " ing "

Why is there an empty string at the start?

I understand that i can use the PREG_SPLIT_NO_EMPTY flag to filter this but it shouldnt be there to begin with. Should it?

  • 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-13T15:51:21+00:00Added an answer on June 13, 2026 at 3:51 pm

    Why shouldn’t it? This is exactly how it works. The semantics of a split operation are that you have a string of this format:

    value-delimiter-value-delimiter-value-...-delimiter-value
    

    (Note that it is starting and ending with a value, not a delimiter.)

    So if your string starts with a delimiter, it is absolutely valid to assume that there is an empty value before that delimiter (since the delimiter is supposed to split something into two). You wouldn’t generally want to reject the empty string between two consecutive ts either, would you?

    And this is exactly what PREG_SPLIT_NO_EMPTY is for. You use it whenever you do want to get rid of those empty strings.

    As a simple example why you would want the default behavior, just think of CSV files. You want to split a line at (for example) ;. You usually also want to allow for empty values. Now if the value in your first column was empty (meaning the line will start with ;, and you chopped that first empty string away completely, then suddenly all indices in the resulting array would correspond to different columns. This is why you want to keep those empty strings as well. In many cases you know how many delimiters there are, and hence how many values – and you want to be able to identify which value belongs at which position. Even if some of them are empty.

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

Sidebar

Related Questions

I'm trying to learn Regex & URL Rewriting in PHP. How can I create
Hey So I am not good with regex right now, trying to learn though,
I'm trying to learn Regex. Currently I'm trying to write a function that parses
I am trying to learn regex. I have the string: $x = 5ft2inches; How
Trying to learn about php's arrays today. I have a set of arrays like
I´m trying to learn C#, coming from a Python/PHP background, and I´m trying to
Trying to learn Regex in Python to find words that have consecutive vowel-consonant or
i'm trying to learn regex parse in javascript. I need to read the text
im trying to learn about regex, and how to use it braking up som
I'm trying to learn RegEx in Ruby, based on what I'm reading in The

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.