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

The Archive Base Latest Questions

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

I’m looking for a way to replace certain strings with an image. All strings

  • 0

I’m looking for a way to replace certain strings with an image. All strings are enclosed in {} and when the code sees the [} what’s inside will be read and what it equals will become the specific image. I haven’t the foggiest how to implement this and was hoping someone could give me an example.

Here’s my example to better explain:

{1} is replaced with an image

So, when the code sees the {} it will activate. Now so it does not interfere with other parts of the code it is limited to 1 character and only certain characters. This is for magic card casting costs to be more specific. So it would be limited too…..

B,U,R,G,W,X,T, 1-99 for example

===============================

So…..something like this?

$image_string = '{R}{R}{R}{3}

$Mana_symbol ='BURGWXT1-99';

$output = preg_replace_all('/\{([' . $Mana_symbol. '])\}/', '<img src="\1.png"/>', $string);

switch ($output)
{
    case ('B'):
        $mana = '<img src = "black_mana.png"/>';
        break;
    case ('U'):
        $mana = '<img src = "blue_mana.png"/>';
        break;
    case ('G'):
        $mana = '<img src = "green_mana.png"/>';
        break;
    case ('R'):
        $mana = '<img src = "red_mana.png"/>';
        break;
    case ('W'):
        $mana = '<img src = "white_mana.png"/>';
        break;
    case ('1'):
        $mana = '<img src = "1_colorless_mana.png"/>';
        break;
    case ('2'):
        $mana = '<img src = "2_colorless_mana.png"/>';
        break;
    case ('3'):
        $mana = '<img src = "3_colorless_mana.png"/>';
        break;

        ....etc....

Should’t I use preg_replace_all since there will be some with multiple instances of this? Like in the case example as the $image_string above it will replace all occurances in the string that match?

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

    Here’s an example using regular expressions (see http://www.php.net/pcre):

    Say your images are called x.png for a tag {x}:

    <?php
    
    $string = '{1} is replaced with an image';
    
    // Use a regular expression
    // The codes below will be placed into a character class
    $validCodes = 'BURGWXT0-9';
    
    // This array contains the image transforms
    $images = array(
        'B' => 'black_mana.png',
        'U' => 'blue_mana.png',
        // ...
    );
    
    // Use preg replace to insert the images
    $string = preg_replace_callback(
        '/\{([' . $validCodes . ']+)\}/',
        function($m) use ($images) {
            if (isset($images[$m[1]])) {
                return '<img src="' . $images[$m[1]] . '"/>';
            }
            return '';
        },
        $string
    );
    
    echo $string;
    
    ?>
    

    Please ask if you need further clarification.

    Edit

    I’ve added a mechanism for you to add your own transforms by populating an array.

    preg_replace and preg_replace_callback will both replace all occurances they find in the string.

    Note that the anonymous function I’ve used is only available in PHP 5.3.0+ (http://php.net/manual/en/functions.anonymous.php).

    Edit 2

    I’ve just realised that the character class for the regex wouldn’t capture all your characters, and that you’d need the + after the character class to capture some of your codes.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.