Sunny day, but I'm inside trying to replace placeholders in a string with words

Hello All,

It’s bright and sunny on the Costa Blanca, and we may even hit 20c today. nice. However I am stuck on a small PHP problem. I want to replace placeholders in a string with words.

The idea is to build an array of placeholders from DB and a corresponding array of placeholder_names and then replace the [placeholders] with the placeholder_names.

Your help will mean I can go out into the sunshine sooner

My code follows:

<?
include "connect.inc.php";

//replace placeholders
$language_id = '1';
$query = "SELECT placeholder_name, placeholder_language_name FROM placeholder, placeholder_language 
WHERE placeholder.placeholder_id = placeholder_language.placeholder_id AND language_id = '$language_id'";
$result = mysql_query($query);
echo $query.'<br>';
$pln_array = array();
$pn_array = array();

while ($placeholder_array = mysql_fetch_assoc($result)) {
$placeholder_id = $array['placeholder_id'];
$placeholder_name = $array['placeholder_name'];

$placeholder_language_name = $array['placeholder_language_name'];

array_push($pln_array,$placeholder_language_name);
array_push($pn_array,$placeholder_name);
}


print_r($pln_array);
print_r($pn_array);


$output = 'once upon a time in the [province] of Alicante, there was a [town] [apartment] 	apartment
[type] 	type
[beds] 	beds
[baths] 	baths
[sell] 	sell
[price from] 	price from
[price to] 	price to
[home] 	home
[contact] 	contact
[register] 	register
[agents] 	agents
[reference] 	reference
[price] 	 ';

$output = str_replace($pln_array,$pln_array,$output);

echo $output;
?>

$keys = array('[type]','[beds]');

$vals = array('apartment','3');

$str = "A beautiful [type] with [beds] beds";

$out = str_replace($keys, $vals, $str);

echo $out;

// A beautiful apartment with 3 beds

Does that help?

EDIT
Interesting topic on Templates at the moment here on SP which might help you …

http://www.sitepoint.com/forums/showthread.php?806031-Creating-a-basic-template-engine

That is what I am trying to do, but how do I get the keys and vals into arrays?

Both are held in the DB.

EDIT: No, nevermind… one second… crayon time!


while ($[COLOR="#FF0000"]placeholder_array[/COLOR] = mysql_fetch_assoc($result)) {
$placeholder_id = $[COLOR="#FF0000"]array[/COLOR]['placeholder_id'];
$placeholder_name = $[COLOR="#FF0000"]array[/COLOR]['placeholder_name'];

$placeholder_language_name = $[COLOR="#FF0000"]array[/COLOR]['placeholder_language_name'];

array_push($pln_array,$placeholder_language_name);
array_push($pn_array,$placeholder_name);
} 

sings One of these things is not like the others…

(Yes, it’s always a Sesame Street Morning in my head.)

I fear I am not understanding this completely, and that the sun is setting …

Sounds as if your ‘keys’, as I have christened them need to be surrounded by brackets eg [beds] if you have not done that you can do it 3 ways:

edit your db and prepend/append the values with ‘keys’ permanently
add the brackets in your sql using "SELECT CONCAT(‘[’,keys,‘]’) as keys … " etc
add the brackets in PHP by running through the array again

Again, not sure this is your problem though.

Thanks Starlion…I was doing it right…ish.

Sorted.