Running a command in a php template

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Glawar
Forum Newbie
Posts: 3
Joined: Wed Dec 30, 2009 8:28 am

Running a command in a php template

Post 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?
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Running a command in a php template

Post by manohoo »

Did you get permission from the author to do this "translation"?
Glawar
Forum Newbie
Posts: 3
Joined: Wed Dec 30, 2009 8:28 am

Re: Running a command in a php template

Post 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.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Running a command in a php template

Post 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
Glawar
Forum Newbie
Posts: 3
Joined: Wed Dec 30, 2009 8:28 am

Re: Running a command in a php template

Post 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.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Running a command in a php template

Post 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
Post Reply