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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:09:03+00:00 2026-06-03T16:09:03+00:00

I have a string in PHP that I would like to split. This string

  • 0

I have a string in PHP that I would like to split. This string is concatenation of ID numbers from the database.

An example of the string is below but it could be much long with each ID separated by “_”:

ID_1_10_11_12

I would like to split the string into the following:

ID_1_10_11_12

ID_1_10_11

ID_1_10

ID_1

Then concatenate them in a new string reversed in order and then separated by spaces:

new string = “ID_1 ID_1_10 ID_1_10_11 ID_1_10_11_12”

I cant figure this out. I have tried exploding the original value into an array by “_” but that just leaves me with the numbers.

I would appreciate any advice on how I should approach this. For reference these ID’s written to the class value of a checkbox so that parent and child values can be grouped and then manipulated by a jquery function.

  • 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-03T16:09:05+00:00Added an answer on June 3, 2026 at 4:09 pm

    Probably not the most elegant way, and it’ll break if there are less than 2 IDs, but this will return the string you asked for:

    $str = "ID_1_10_11_12";
    
    //creates an array with all elements
    $arr = explode("_", $str);
    
    $new_str = ' ID_' . $arr[1];
    for ($i=2; $i<count($arr); $i++)
    {
        $tmp =  explode(" ", $new_str);
        $new_str .= " " . $tmp[$i-1] . "_" . $arr[$i];
    }
    $new_str = trim($new_str);
    
    echo $new_str; //echoes ID_1 ID_1_10 ID_1_10_11 ID_1_10_11_12
    

    I don’t see much usability for it but there you go.

    You can then simply explode(" ", $new_str) and you’ll also have an array with all the elements in that string, which you can transverse the way you want.

    Obviously, you can also add a if (count($arr) < 3) before the for to check if there are less than 2 array elements after the ID and exit the function printing the $new_str without the white space with trim($new_str) if inputting less than 2 ID arrays is a concern.

    edit: Trimmed the left white space.

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

Sidebar

Related Questions

I have an url like this: http://www.w3schools.com/PHP/func_string_str_split.asp I want to split that url to
I am returning results from a MySql database in PHP that I would like
I have a string encrypted in PHP that I would like to decrypt in
Possible Duplicate: remove duplicate from string in PHP Have a string like: $str =
I have a simple feedback form PHP script that I would like to enhance
I have this function that takes some user-submitted HTML code from the database: function
I have limited JS experience, and I need to split a string that would
I have a stored procedure that I would like to pass a string to,
I have a string that I would like to compress using GZip before inserting
I have a string of HTML that I would like to check to see

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.