str_replace(array) Not Working as Expected with CSS File

Digital Jedi

Well-known member
I don't actually know where any thread goes in this forum. 😶

Is this Stack Exchange post example still possible? I've actually implemented it and the results have been...buggy.

Alternatively you create the base .css file and swap in the parameters using string_replace or something similar. I guess you will want to do multiple replacements at once, so see the 'strtr' function, its basically a string replace but takes in an array of key value pairs as replacement tokens and values.

base.css
CSS:
.selector { background-color: [*backgroundColorPlaceholder*]; }

PHP
PHP:
$cssString = file_get_contents('base.css');
$cssString = str_replace('[*backgroundColorPlaceholder*], $userSubmittedBackgroundColor, $cssString);
file_put_contents('populated.css', $cssString);

The thing is, I would like to use this with an array, but the results either do nothing, or put the replacement of one placeholder in the other's. Is this just not doable? The variables here are coming from an API.

Here's the code for str_replace:
PHP:
$cssParams = file_get_contents('ycard.css');
$cssParams = str_replace(
        array('[*frametype*]', $frametype, $cssParams),
        array('[*set_name*]', $Fset_name, $cssParams),
        array('[*set_code*]', $setcode, $cssParams),
        array('[*name*]', $Fname, $cssParams),
        $cssParams);
file_put_contents('ycard.css', $cssParams);

And here's the CSS with the placeholders:
CSS:
.ygo-card-container.ygo-card-container-[*frametype*] {
    background: url(cardbacks/[*frametype*].png), url(cards/[*setname*]/[*setcode]-[*name*].jpg);
    background-size: contain, 326px;
    background-position: center, center 109px;
    background-repeat: no-repeat;

Actually, if I use all four arrays above it just breaks. But if I strip out last arrays I can get it to load, though, this is the wonky output I get.
CSS:
.ygo-card-container.ygo-card-container-[*set_name*] {
    background: url(cardbacks/[*set_name*].png), url(cards/[*set_name*]/[*set_code]-[*name*].jpg);
    background-size: contain, 36px;
    background-position: center, center 109px;
    background-repeat: no-repeat;
}

It replaces the first two [*placeholders*] with the second [*placeholder*] listed in the second array. Wut? 😐

I feel like this should be working. What am I missing?

Note that this gives me the exact same results. set_name is not even there.
PHP:
$cssParams = file_get_contents('ycard.css');
$cssParams = str_replace('[*frametype*]', $frametype, $cssParams);
file_put_contents('ycard.css', $cssParams);

EDIT: I just discovered that it is replacing the first three [*placeholders*] with an unrelated variable.
PHP:
$cssParams = file_get_contents('ycard.css');
$cssParams = str_replace('[*set_name*]', $Fname, $cssParams);
file_put_contents('ycard.css', $cssParams);

Output:
CSS:
.ygo-card-container.ygo-card-container-royal-oppression {
    background: url(cardbacks/royal-oppression.png), url(cards/royal-oppression/[*set_code]-[*name*].jpg);
    background-size: contain, 36px;
    background-position: center, center 109px;

This is the content of $name. Which isn't even part of the str_replace. Is this some kind of caching issue?
 
Last edited:
Top Bottom