php - read file to array

Soldato
Joined
19 Oct 2002
Posts
3,480
i can't seem to find a neat way of reading a file into an array line by line...

all the examples on the net are really messy and overkilled, basically, i have a text file with a tag on each line:

<img tag="xyz.jpg">
<img tag="123.jpg">
<img tag="abc.jpg">

and that is all thats in the file...

i want some php to look at this file and put the first line in array[0] the second in array[1] etc...

there seems to be lots of ways of doing it that include fgets, explode, and loads of other functions, could someone just show me the slickest way to do this i'm sure it doesn't need to be as complicated as everyone makes it look...

any ideas?

wont something dead simple like this work?

Code:
<?php 
$myFile = "YourFile.txt"; 
$handle = fopen($myFile, 'r'); 
while (!feof($handle)) 
{ 
$data[] = fgets($handle, 512);
} 
fclose($handle); 
?>
 
Last edited:
thats ok cuz the file is local, but that works brilliantly i was overkilling it bigtime, such a slick little function :)

could i sneak in another question?:

i am trying to take every line, and trim off the tag information leaving just the filename... so far i have this but it doesnt work... any ideas why?

Code:
<?php 

$images = file('gallery-extensions.html');

$tags = array('<img class="clienteditor" title="Gallery Item" src="', '" />');

function tagTrim($line)
{
	$line = str_replace($tags, "", $line);
}

array_walk($images,'tagTrim');

?>

from what i can deduce with echos, the lines of the file aren't making it into the tagTrim function, so the str_replace isn't getting to do its job... so my issue i'm guessing is with array_walk but i've followed the right syntax so am a bit foxed...
 
Last edited:
Again, from the same page I linked you to (:rolleyes:):

Code:
for ( $i = 0 ; $i < count($images) ; $i++ )
{
    $images[$i] = tagTrim($images[$i]);
}
 
Again, from the same page I linked you to (:rolleyes:):

Code:
for ( $i = 0 ; $i < count($images) ; $i++ )
{
    $images[$i] = tagTrim($images[$i]);
}
ok, i was going to try it like that originally but i found this array_walk function and it sounded much more concise...

found out that i have to pass the value by reference which means the function is allowed to edit the value... but it still doesn't work look:

Code:
<?php 

$images = file('gallery-extensions.html');

$tags = array('<img src="', '" />');

function tagTrim(&$line)
{
	$line = str_replace($tags, "", $line);
}

array_walk(&$images,'tagTrim');

?>

(if i can't get it to work, i'll use the for loop but this seems much nicer no?)
 
i'm avvin a mare with this... i need to get it done and although i would prefer the slicker array_walk method i just wanna get it working...

i've used the for loop, and the bugger STILL doesn't work could anyone help me out?

Code:
<?php 

$images = file('gallery-extensions.html');

$tags = array('<img src="', '" />');

function tagTrim($line)
{
	$line = str_replace($tags, "", $line);
	return $line;
}

for ( $i = 0 ; $i < count($images) ; $i++ )
{
    $images[$i] = tagTrim($images[$i]);
}

//array_walk(&$images,'tagTrim');

?>
 
what's the point of this text file exactly? if these image files are on the local server, just use glob to create an array of the filenames.

PHP:
$images = glob('*.jpg'); // reads all jpg files in current folder into $images array
 
right i've managed it, the reason it wasn't working was that the $tags variable needed to be inside the function... dumbass!

mark, i dont think that would work for me cuz i need to retrieve only the files (and paths) of images that are in a text file, glob kinda gives a directory listing with sizes and stuff doesn't it?
 
the reason i suggested glob because it gives exactly what you were trying to extract from the textfile. just the filename which would be stored in each element of the array. eg

PHP:
$images = glob('*.jpg');

echo images[0]; // will display 'a.jpg'
echo images[1]; // will display 'b.jpg' etc
 
Back
Top Bottom