Replace string with image

Go To StackoverFlow.com

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?

2012-04-03 23:24
by Justin Lucas
Are the strings read in from a db or file or user input or are they embedded within existing HTML or PHP? As far as what replaces the values within the {} do you have some sort of map or lookup, or is this something stored in a database table - TheOx 2012-04-03 23:29
The information is being pulled from an XML file and then the variables are assigned and posts are automatically created from the info pulled - Justin Lucas 2012-04-03 23:38


3

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.

2012-04-03 23:35
by Pete
I've added my response to your answer in the original post above as it was too big to put in comments : - Justin Lucas 2012-04-03 23:55
See updated answer - Pete 2012-04-04 00:45
Hi Pete this deletes the entire description area when inserting product data. Would you mind taking a look at this code? http://codepad.org/okkOKaIj ....And I'm also running PHP 5.3.10 and the anonymous function works fine - Justin Lucas 2012-04-04 09:48
The 3rd parameter of preg_replace_callback needs to be $ability in your code - you're passing it an empty string - Pete 2012-04-04 10:48
ah Doh! I fixed that, but the code doesn't return images it eithe returns nothing or it returns (,)....example {T} returns (,) and {2} returns nothing.... - Justin Lucas 2012-04-04 11:09
I've just run the code from your previous listing, with the $ability change, and it works as expected for me - try var_dump($ability); after the replace to check the contents of the string - Pete 2012-04-04 12:23
Found it. I forgot to remove wpstripalltags for $ability in array for wpinsert_post. Thanks for the help Pete : - Justin Lucas 2012-04-04 12:33
So far they look fine even in instances where its {W}{W},{T}: blah blah blah - Justin Lucas 2012-04-04 12:35


1

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)

2012-04-03 23:37
by Darrell Ding
Ads