Justin Lucas
Justin Lucas

Reputation: 319

Replace string with image

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?

Upvotes: 0

Views: 1796

Answers (2)

Pete
Pete

Reputation: 1554

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.

Upvotes: 3

Darrell Ding
Darrell Ding

Reputation: 73

Youu could use a regular expression to parse all the {}'s out then str_replace them later

preg_match_all('/{(1|2|3|99|a)}/', "{1} is replaced with an image{a} {99}", $match)

Upvotes: 1

Related Questions