Forum Moderators: coopster

Message Too Old, No Replies

Script for inserting code

help plx

         

Zenden

4:06 am on Apr 2, 2005 (gmt 0)

10+ Year Member



i want to make a script, in wich i write a piece of xml code and it creates it in a specified line of a xml file

some1 know howto do it?

mcibor

12:07 pm on Apr 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is an exemplary code that creates sth in xml

<?php
$xml = "<?xml version=\"1.0\" encoding=\"iso-8859-2\"?>";//beginning of the xml
$xml .= "\n\r<BOOK>\n\r\t<Author>P.K.Dick</Author>\n\r\t<Title>Ubique</Title>\n\r</BOOK>";
if($fd = @fopen("books.xml", "w")) // open the desired xml file
{
fwrite($fd, $xml); //write the text to the file
fclose($fd); // you need to always close the file after writing to it.
}
?>

To do what you written you want to do is: read the xml (read as many lines as you wish, but count them), if you are on the desired line write the code, then read the rest of the file and write the variable (to which you have read the file) to the file.

Script to instert $text after certain line (not on the beginning of the file), doesn't check for the length of the file.

<?php
$f = @fopen("books.xml", "r");
$i = 0; beginning values
$line_no = 10; //after which line you want to instert text;
$text = "Text to insert";

while(!feof($f))
{
$line .= trim(fgets($f));
if($++i == $line_no) $line .= $text;
}
fclose($f);
$f2 = @fopen("books.xml", "w");
fwrite($f2, $line);
fclose($f2);
$line="";//to flush the memory
?>

Best regards
Michal Cibor

[edited by: coopster at 3:14 pm (utc) on April 2, 2005]
[edit reason] joined two posts upon request [/edit]

Zenden

8:54 pm on Apr 2, 2005 (gmt 0)

10+ Year Member



ok, but i want that the created text to be a variable.
a $_POST like
$name= $_POST['name']
and the text to be writen is
<character name="' . $name . '" />
obs: this xmls files are part f a online game
if u want to understand better wat i want to do is the following

the user is gonna write 3 fields, the first is the name of the .xml the second is a value inside the .xml(like a password) and the third is what he wants to wite inside it.

Zenden

4:54 am on Apr 3, 2005 (gmt 0)

10+ Year Member



and the password is alredy gonna be in the xml file
so if the the file name the user entered doesnt have the "password inside" it wont create the code in the line
can some of u guys help me?