User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,628 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,780 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 717 | Replies: 3
Reply
Join Date: Apr 2007
Posts: 12
Reputation: MaestroS is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
MaestroS MaestroS is offline Offline
Newbie Poster

XML and str_replace !

  #1  
May 5th, 2007
Well, I were trying to make code that gets a XML element from the file and replaces with new data.

Here is my code.

Parsing command:
function ExecuteCMD($forplayer, $what, $to)
{
$player_dir = 'D:\\Programy\\Open Tibia Server Project\\DeadTouch Binary\\data\\players\\';

$SetPlayer = file_get_contents($player_dir . "\\" . $forplayer . ".xml");
$SetPlayer = str_replace($what, $to, $SetPlayer);

$OpenFile = fopen($player_dir . "\\" . $forplayer . ".xml", "w+");
if ($OpenFile)
{
fwrite($OpenFile,$SetPlayer);
}
fclose($OpenFile);
}

Here is command that function above executed:
require_once('./config.php');
require_once('./pe_ha_pe.php');
$cmd = explode(" ", $_POST['cmd']);
$player_dir = $data . '\\players\\';

@ $acp_d_file = file_get_contents($player_dir . "\\" . $cmd[1] . ".xml");
@ $System = simplexml_load_string($acp_d_file);

if ($cmd[0] == "/level")
{
if (!$cmd[1] || !$cmd[2])
{
echo 'Not enough of parameters for <B>' . $cmd[0] . '</b> command...';
}
else
{
if (@ !$acp_o_file = fopen($player_dir . "\\" . $cmd[1] . ".xml", "r"))
{
echo 'Player does not exist or nickname is wrong.';
}
else
{

if ($System['level'] == $cmd[2])
{
echo 'Provided level and player level is same.';
}
else
{
ExecuteCMD($cmd[1], $System['level'], $cmd[2]);
echo 'Command ' . $cmd[0] . ' with value ' . $cmd[2] . ' used on ' . $cmd[1] . ': <font color="green">SUCCESS</font>';
}
}

}

}

And for example, if I execute command:
/level GOD 0

In GOD.xml we'll have:
<?xml version="0.0"?>
<player name="MaestroS" account="0152818" sex="0" lookdir="0" exp="015544804375" voc="01" level="01439" access="0" cap="09810" maglevel="0139" lastlogin="01178140167" lastlogout="01178132522" promoted="0" banned="0">
(...)

When it should look like that:
<?xml version="1.0"?>
<player name="MaestroS" account="152818" sex="0" lookdir="0" exp="15544804375" voc="1" level="0" access="0" cap="9810" maglevel="139" lastlogin="1178140167" lastlogout="1178132522" promoted="1" banned="0">
(...)


The problem is in ExecuteCMD function.
I think it's about str_replace that just works wrong.

How to get an element from file and replace with new provided data correctly ?
Last edited by MaestroS : May 5th, 2007 at 5:59 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: XML and str_replace !

  #2  
May 6th, 2007
What you _could_ do is..
say the XML element is <item>...
You could store all text before and including the <item> tag.

Then store all the text after and including the </item> tag.

Then you can do.

$string = $before . $value . $after;
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote  
Join Date: Dec 2006
Location: syria
Posts: 157
Reputation: w_3rabi is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 8
w_3rabi's Avatar
w_3rabi w_3rabi is offline Offline
Junior Poster

Re: XML and str_replace !

  #3  
May 8th, 2007
or you could load the data into an array
and do your changes
then write all the array to the file
programming is an art ,only for those who can understand it.
- th3 php wr3nch -
Reply With Quote  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: XML and str_replace !

  #4  
May 8th, 2007
That would probably be an easier approach.
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 12:54 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC