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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:25:23+00:00 2026-05-28T17:25:23+00:00

I am trying to use regex in PHP to parse a string and make

  • 0

I am trying to use regex in PHP to parse a string and make an array. Here is my attempt:

function parseItemName($itemname) {
    // Given item name like "2 of #222001." or "3 of #222001; 5 of #222002."
    // Return array like (222001 => 2) or (222001 => 3, 222002 => 5)
    preg_match('/([0-9]?) of #([0-9]?)(.|;\s)/', $itemname, $matches);
    return $matches;
}

Calling the function with

print_r(parseItemName("3 of #222001; 5 of #222002."));

returns

Array ( [0] => 3 of #22 [1] => 3 [2] => 2 [3] => 2 )

Does anyone know how to make this work? I assume that preg_match() is not the best way to do this, but I’m not sure what else to try. I appreciate any ideas. Thank you!

  • 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-28T17:25:24+00:00Added an answer on May 28, 2026 at 5:25 pm

    Aside from the adjustment to your regex pattern, you want to be using preg_match_all with the PREG_SET_ORDER flag set to make things simpler.

    This will return a $matches array arranged like so:

    array
      0 => 
        array
          0 => string '3 of #222001' (length=12)
          1 => string '3' (length=1)
          2 => string '222001' (length=6)
      1 => 
        array
          0 => string '5 of #222002' (length=12)
          1 => string '5' (length=1)
          2 => string '222002' (length=6)
    

    The below example function now loops through all of the matches, and constructs a new array, using the second match as the key, and the first match as the value.

    function parseItemName($itemname) {
        // Given item name like "2 of #222001." or "3 of #222001; 5 of #222002."
        // Return array like (222001 => 2) or (222001 => 3, 222002 => 5)
        preg_match_all('/(\d+)\sof\s#(\d+)/', $itemname, $matches, PREG_SET_ORDER);
    
        $newArray = array();
    
        foreach($matches as $match) {
            $newArray[$match[2]] = intval($match[1]);
        }
    
        return $newArray;
    }
    
    var_dump(parseItemName("3 of #222001; 5 of #222002."));
    

    The output that is dumped will look like this:

    array
      222001 => int 3
      222002 => int 5
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to come=up with a regex string to use with the PHP preg
I am trying to use PHP preg_math parse a string that looks like: 6.15608128
I am getting some problems while trying to use this regex: (public +function|private +function|function)
so I am trying to form a PCRE regex in php, specifically for use
I'm stuck trying to make a regex in PHP that catches the link and
I've been trying to use grubers latest url matching regex in a php project.
trying to use regex to replace any white space with  , inside of example
I'm trying to use a regex in Python to match a file (saved as
I'm trying to use Boost regex to see if something has an integer in
I am trying to use this regex (JS): /\/\*(.*)\*\//g To replace /* sdandsads */

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.