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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:15:49+00:00 2026-05-27T02:15:49+00:00

I am trying to split/explode/preg_split a string but I want to keep the delimiter

  • 0

I am trying to split/explode/preg_split a string but I want to keep the delimiter example :

explode('/block/', '/block/2/page/2/block/3/page/4');

Expected result :

array('/block/2/page/2', '/block/3/page/4');

Not sure if I have to loop and then re-prefix the array values or if there is a cleaner way.

I have tried preg_split() with PREG_SPLIT_DELIM_CAPTURE but I get something along the lines of :

array('/block/, 2/page/2', '/block/, 3/page/4');

Which is not what I want. Any help is much appreciated.

  • 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-27T02:15:50+00:00Added an answer on May 27, 2026 at 2:15 am

    You can use preg_match_all like so:

    $matches = array();
    preg_match_all('/(\/block\/[0-9]+\/page\/[0-9]+)/', '/block/2/page/2/block/3/page/4', $matches);
    var_dump( $matches[0]);
    

    Output:

    array(2) {
      [0]=>
      string(15) "/block/2/page/2"
      [1]=>
      string(15) "/block/3/page/4"
    }
    

    Demo

    Edit: This is the best I could do with preg_split.

    $array = preg_split('#(/block/)#', '/block/2/page/2/block/3/page/4', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
    
    $result = array();
    for( $i = 0, $count = count( $array); $i < $count; $i += 2)
    {
        $result[] = $array[$i] . $array[$i + 1];   
    }
    

    It’s not worth the overhead to use a regular expression if you still need to loop to prepend the delimiter. Just use explode and prepend the delimiter yourself:

    $delimiter = '/block/'; $results = array();
    foreach( explode( $delimiter, '/block/2/page/2/block/3/page/4') as $entry)
    {
        if( !empty( $entry))
        {
            $results[] = $delimiter . $entry;
        }
    }
    

    Demo

    Final Edit: Solved! Here is the solution using one regex, preg_split, and PREG_SPLIT_DELIM_CAPTURE

    $regex = '#(/block/(?:\w+/?)+(?=/block/))#';
    $flags = PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY;
    preg_split( $regex, '/block/2/page/2/block/3/page/4', -1, $flags);
    preg_split( $regex, '/block/2/page/2/order/title/sort/asc/block/3/page/4', -1, $flags);
    

    Output:

    array(2) {
      [0]=>
      string(15) "/block/2/page/2"
      [1]=>
      string(15) "/block/3/page/4"
    }
    array(2) {
      [0]=>
      string(36) "/block/2/page/2/order/title/sort/asc"
      [1]=>
      string(15) "/block/3/page/4"
    }
    

    Final Demo

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

Sidebar

Related Questions

I am trying to split a text into an array using explode, but for
I'm trying to split a string into an array of words, however I want
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I'm trying to split a string on its punctuation, but the string may contain
I'm trying to split a string into a int list for further processing. But
I am trying to split a string into an array of word pairs in
Im trying to split a String into an Array. In PHP I normally use
I'm trying to split a string that looks like this for example: ':foo [bar]'
I'm trying to split a string received from a $_GET in PHP, but I'm
I'm trying to use either split, preg_split, or explode to parse the following data

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.