str_replace() function and multiple variables?
This is going to be pretty hard to explain, so I'll try to make it as much of a chronological story as possible and end with the question, so that anyone who needs a relatively in-depth idea of what I'm talking about has one :).
I have a theme template with four text containers named primary_headline
, primary_subline
, secondary_headline
and secondary_subtext
. The content for each container comes from echo($container_name)
.
Each container has it's own variable, $container_name
, which posts user created content to from a form, contents which sometimes contains the variables $city_name
and $ref_name
as raw data (i.e. "Hello, I'm from $city_name"), as $city_name
and开发者_开发技巧 $ref_name
has assigned values, for arguments sake let's just say $city_name
= Dallas
and $ref_name
= Facebook
.
Originally, I believed that I only wanted the user to be able to use $city_name
and $ref_name
within the secondary_subtext
container, and I was also going to make the option to use $city_name
OR $ref_name
the choice of the user in the first place (they'd have to select one, or the other, or none - but couldn't select both.
However, I now want to allow $city_name
AND $ref_name
across all four container variables ($secondary_subtext
for example).
So, my question is, how do I go about doing that in the easiest possible fashion?
Here's my old code for good measure:
if($geo_text == "enable") {
$geo_init = include("inc/geo_text.php");
$secondary_headline = str_replace('$city_name', $city_name, $_POST['secondary_headline']); // $_POST data from user
} else($ref_text == "enable") {
$ref_init = include("inc/ref_text.php");
$secondary_headline = str_replace('$ref_name', $ref_name, $_POST['secondary_headline']); // $_POST data from user
} else { $secondary_headline = $_POST['secondary_headline']; }
Any comments/answers etc will be very greatly appreciated :)!!
$placeholders = array('city_name','ref_name');
$include_mapper('geo_text' => 'inc/geo_text.php','ref_text'=>'inc/ref_text.php');
$containers = array('first_headline','scondary_headline');
$found = 0;
foreach ($include_mapper as $map => $include){
if ($$map == "enable"){
include ($include_mapper[$map]);
foreach ($container as $container){
foreach ($placeholders as $placeholder){
$$container = str_replace('$'.$placeholder, $$placeholder, $_POST[$container];
}
}
$found = 1;
break;
}
}
if (!$found){
foreach ($container as $container){
$$container = $_POST[$container];
}
}
精彩评论