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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:52:31+00:00 2026-06-12T20:52:31+00:00

With PHP if you have a string which may or may not have spaces

  • 0

With PHP if you have a string which may or may not have spaces after the dot, such as:

"1. one 2.too 3. free 4. for 5.five "

What function can you use to create an array as follows:

array(1 => "one", 2 => "too", 3 => "free", 4 => "for", 5 => "five") 

with the key being the list item number (e.g the array above has no 0)

I presume a regular expression is needed and perhaps use of preg_split or similar? I’m terrible at regular expressions so any help would be greatly 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-06-12T20:52:32+00:00Added an answer on June 12, 2026 at 8:52 pm

    I got a quick hack and it seems to be working fine for me

            $string = "1. one 2.too 3. free 4. for 5.five ";
    
        $text_only = preg_replace("/[^A-Z,a-z]/",".",$string);
        $num_only = preg_replace("/[^0-9]/",".",$string);
    
        $explode_nums = explode('.',$num_only);
        $explode_text = explode('.',$text_only);
    
        foreach($explode_text as $key => $value)
        {
            if($value !== '' && $value !== ' ')
            {
                $text_array[] = $value;
            }
        }
    
        foreach($explode_nums as $key => $value)
        {
            if($value !== '' && $value !== ' ')
            {
                $num_array[] = $value;
            }
        }
    
        foreach($num_array as $key => $value)
        {
            $new_array[$value] = $text_array[$key];
        }
    
        print_r($new_array);
    

    Test it out and let me know if works fine

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

Sidebar

Related Questions

Short question you guys: I have a PHP string which may or maybe not
I have a PHP string type variable which may come encoded in Hexadecimal pattern
I'm pretty new to PHP and have a String which contains an HTML page.
I want to have a string variable for a PHP class, which would be
I have some code validating a string of 1 to 32 characters, which may
We have a function used within our PHP/MySQL application which returns basic configuration information,
I have a string from which I need to capture one and possibly two
I have a array stored in a PHP file in which I am storing
I have the following code: <div><strong>Name: </strong><?php echo anchor('http://'.$link, $row->Name); ?></div> Which takes a
Possible Duplicate: remove duplicate from string in PHP Have a string like: $str =

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.