Page 1 of 1

Running a command in a php template

Posted: Wed Dec 30, 2009 8:42 am
by Glawar
Greetings all, my first time here on php developer network...

I'm translating and otherwise improving a German game that has all its php files encrypted, but has many open php template files.

Whenever a player joins a game, he gets a village with name $village.name, which is equivalent to $player.name + "s dorf". If his username is Aragorn, the value of $village.name is "Aragorns dorf". I need to have this variable be automatically changed to $player.name + "s village" (dorf is the german word for village).

Luckily, there is an option within the game that allows the user to change the name of his village. Here's that code (from one of the template files):

Code: Select all

<form action="game.php?village={$village.id}&screen=main&action=change_name&h={$hkey}" method="post">
<table>
<tr><th colspan="3">Change village name:</th></tr>
<tr><td><input type="text" name="name" value="{$village.name}"></td><td><input type="submit" value="Change"></tr>
</table>
</form>
So what I'd like to do is put some code at the top of the template that checks to see if the name has "dorf" in it, and if so, changes the name to $player.name + "s village". I'm guessing I need to somehow call the game.php file with h={$player.name + "s village"}.

Suggestions?

Re: Running a command in a php template

Posted: Wed Dec 30, 2009 11:52 am
by manohoo
Did you get permission from the author to do this "translation"?

Re: Running a command in a php template

Posted: Thu Dec 31, 2009 10:33 am
by Glawar
In general, the authors of the game allow translation of the unencrypted code, but we are forbidden to try to decode what is encrypted.

Re: Running a command in a php template

Posted: Thu Dec 31, 2009 1:47 pm
by manohoo
Try this:

Code: Select all

$village_name = "Peter's dorf";
$village_name = trim($village_name); // trim spaces
 
// substr_replace(string,replacement,start,length)  
$village_name = substr_replace($village_name, "village", strlen($village_name) - 4); // length of "dorf" is 4
 
echo $village_name;
 
will output:
Peter's village

Re: Running a command in a php template

Posted: Thu Dec 31, 2009 1:57 pm
by Glawar
Thanks...

I can't figure out how to make that php code run in the template to actually change the value of the variable.

Re: Running a command in a php template

Posted: Thu Dec 31, 2009 5:19 pm
by manohoo
1. The form prompts the user to "Change village name"
2. The user enters "Peter" and clicks on the button "Change".
3. "Peter" is now stored in $_POST['name']). Notice that there's no "dorf" or "village", just "Peter"
4. The form is processed by file game.php.

I don't know the code of game.php, but this is what I would do to change the village name to "Peter's village"

game.php should include this code:

Code: Select all

$village_name = $_POST['name'];
$village_name = trim($village_name)."'s village";
echo "\$village_name = ".$village_name; // this line to be deleted