PDA

View Full Version : split .txt content in php



2nd day
January 11th, 2005, 03:24 AM
hi guys,

I'm making a photogallery with discription and i want to be able to edit my discription online. I've got a .txt file with content like this:
&filename.jpg=this is the discription of the pic
&anotherfile.jpg=this is another discription

let's say this .txt file has the name text.txt. In php i include this file (include("this.txt");) then the content of my file is shown. I want to split this content like an array... something like this $array = array("filename.jpg" => "this is the discription of the pic", "anotherfile.jpg" => "this is another discription");
so without the &character...

the problem is i don't know how to split all this, so i need some help with that please...

Gazler
January 11th, 2005, 06:27 PM
$L = strpos($file,"-");
$file=str_replace('-', '', $file)
$filename= (left($file, $L));
$file=str_replace($filename, '', $file)

Then you'd insert filename and file into the array.

Cheers, Gaz.

You'll need this function

Function left($leftstring, $leftlength) {
return(substr($leftstring, 0, $leftlength));
}

2nd day
January 12th, 2005, 08:40 AM
i don't completely get it... where should i define my file?

Gazler
January 12th, 2005, 03:11 PM
Didn't understand that, but to open the file you do.



$f= "output.txt";
$filename = fopen ($f, "r+");


before the above.

2nd day
January 12th, 2005, 03:14 PM
what is the complete script? because $file isn't determined...

2nd day
January 12th, 2005, 03:16 PM
<?php

$f= "beschrijvingen.txt";
$filename = fopen ($f, "r+");

$L = strpos($file,"-");
$file=str_replace('-', '', $file)
$filename= (left($file, $L));
$file=str_replace($filename, '', $file)


Function left($leftstring, $leftlength) {
return(substr($leftstring, 0, $leftlength));
}

?>

this is what i have now but it doesnt work...

Gazler
January 12th, 2005, 03:22 PM
my mistake, $file= fopen($f, "r+")

2nd day
January 12th, 2005, 03:31 PM
it doesn't make an array does it?

lostinbeta
January 12th, 2005, 03:39 PM
Check out the explode() (http://us4.php.net/manual/en/function.explode.php) function in PHP.

It works like split() in actionscript.

Gazler
January 12th, 2005, 04:06 PM
No, it doesn't make an array, I thought you knew how to do that.

<?php

$f= "beschrijvingen.txt";
$fole= fopen ($f, "r+");
$array = array();

for ($i=0; $i<NUMBER OF DESCRIPTIONS IN TXT FILE; $i++)
{
$L = strpos($file,"=");
$file=str_replace('=', '', left($file, $L+1))
$filename= (left($file, $L));
$file=str_replace($filename, '', $file)
$L = strpos($file, "&");
$file=str_replace('&', '', left($file, $L+1))
$filedesc = (left($file, $L));

$array[i] = $filename;
$array[i+1] = $filedesc;
}
Function left($leftstring, $leftlength) {
return(substr($leftstring, 0, $leftlength));
}

?>

That's the whole code, it should work.

2nd day
January 13th, 2005, 11:24 AM
thanks it does work now :D