Substituting wild card for similar values in an array

I have a script that displays the home page URL for a series of websites both locally and online. For example, the value for $MySite_URL on my laptop might be MS, while the value online might be www.mysite.com.

When inserted in text, the values look like this:


(See <a href="<php echo $MS_URL; ?>">MySite.com</a>)

But now that I’m putting my text into database tables, those PHP scripts don’t work. So I replaced the echo value with this:

(See <a href=“$MS_URL”>MySite.com</a>)

Then I just use str_replace to replace $MS_URL with the php code, like this…


$Text = str_replace($URL_That, $URL_This, $Text);

And here’s what my array looks like:


$URL_That = array('$G_URL', '$PX_URL');
$URL_This = array(''.$G_URL.'', ''.$PX_URL.'');

So here’s my question: I have a lot of websites to put in this array and will have to add more as I create more websites. Since every value ends with _URL, is there a PHP or regex code I can use to shorten this to a single “floating value”? I’m thinking of something like this:


$URL_That = array('$*_URL');
$URL_This = array(''.$*_URL.'');

All the website ID’s consist of one, two or three characters followed by _URL: G_URL, GW_URL, BBW_URL, DB_URL. All the letters are capitalized. However, a few contain numerals, usually a 2: URL_BR2.

I thought I might be able to use some variation of this script, which includes a file if a URL = a single capital letter, but I haven’t figured out how to rewrite it:


if (preg_match("/^[A-Z]$/", $MyURL)) {
 $GlossarySeries = 'Alphabet';
 [include something]
}

 else {
 include_once($BaseINC."/404.php");
    }

Thanks for any tips.

See http://stackoverflow.com/questions/9142035/preg-replace-using-the-replaced-text-as-variable-name

The solution given there was to use preg_replace with the e modifier (for eval) and using the concept of variable variables. That modifier is deprecated as of PHP 5.5 so take that into consideration.

Another way to go would be preg_replace_callback() but then all your variables ($G_URL, $MS_URL, etc.) would need to be accessible inside your callback function and that may require some major rewriting of the rest of your code. I’m not entirely clear on why you’ll have so many of these variables defined but you’re keeping other data in a database now. Could be there’s a very different way to handle this whole thing, but those are two solutions which would get the job done as it is.