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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:19:12+00:00 2026-06-01T18:19:12+00:00

For instance I have this piece of data: array( 1 => ‘Metallica’, 2 =>

  • 0

For instance I have this piece of data:

array(
    1 => 'Metallica',
    2 => 'Megadeth',
    3 => 'Anthrax',
    4 => 'Slayer',
    5 => 'Black Sabbath',
);

And I have this piece of text:

My first favorite band is: #band{2}, and after that it’s: #band1. My
over-all first metal band was: #band{5}, and I sometimes enjoy
headbaning while listening to: #band3 or #band{4}.

So after the RegEx, it should look like this:

My first favorite band is: Megadeth, and after that it’s: Metallica.
My over-all first metal band was: Black Sabbath, and I sometimes enjoy
headbaning while listening to: Anthrax or Slayer.

So, I need a pattern/example how can I extract numbers from these two patterns:

#band{NUMERIC-ID} or #bandNUMERIC-ID

  • 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-01T18:19:13+00:00Added an answer on June 1, 2026 at 6:19 pm

    No need for regular expressions, just use str_replace():

    $map = array();
    foreach ($bands as $k => $v){
        $map["#band".$k] = $v;
        $map["#band{".$k."}"] = $v;
    }
    
    $out = str_replace(array_keys($map), $map, $text);
    

    A demo: http://codepad.org/uPqGXGg6

    If you want to use regular expressions:

    $out = preg_replace_callback('!\#band((\d+)|(\{(\d+)\}))?!', 'replace_band', $text);
    
    function replace_band($m){
        $band = $GLOBALS['bands'][$m[2].$m[4]];
        return $band ? $band : 'UNKNOWN BAND';
    }
    

    A demo: http://codepad.org/2hNEqiCk

    [edit] updated for multiple forms of the token to replace

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

Sidebar

Related Questions

I have this data in a table, for instance, id name parent parent_id 1
I have this object which is an instance of a superclass. I want to
I have this string for an instance: 2 + 2 - 2 and when
I have this string : <meis xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance uri=localhost/naro-nei onded=flpSW531213 identi=lemenia id=75 lastStop=bendi xsi:noNamespaceSchemaLocation=http://localhost/xsd/postat.xsd xsd/postat.xsd>
Okay, I have this program and I don't want more than one instance of
For instance I have a string like this : abc123[*]xyz[#]098[~]f9e [*] , [#] and
I need to have a single instance application (as per this answer ), but
I have to share an instance of a config object in PHP. This object
I have to take a piece of data, and apply a large number of
I have an array of point data, the values of points are represented as

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.