Page 1 of 1

Quickest way to retrieve filenames

Posted: Wed May 19, 2004 2:09 am
by maqmus
I have a file where I store data like this:

Code: Select all

<?php
<img src="somefile.gif" width="35" heigth="65">\n
<a href="somepage.html" target="_blank">My link</a>\n
<object.....(the whole code for flash files) ="myflashfile.swf" ...object>
?>
Now I'll delete a line, but I want to delete the used file (somefile.gif or myflashfile.swf). So I need to retrieve the filename from the array.
I'll be using only 4 sufixes: .html, .gif, .jpg and .swf.

What should be the quickest way to do that?

P.S. Before the "n" should be a backslash, but I can't get it to print it out.

Posted: Wed May 19, 2004 2:38 am
by feyd
try this:

Code: Select all

preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches);
// $matches contains the .. matches  
print_r($matches);

Posted: Wed May 19, 2004 3:16 am
by maqmus
I tried this but I get an Array and I can't read anything from it.
This is what I did:

Code: Select all

<?php
preg_match("#="?((/?[a-z0-9_-]+/?)+\.(gif|jpg|swf))"?#i",$line[1],$matches); 
print_r($matches);

foreach($matches as $esto) {
echo $esto;
}
?>
This way I get nothing.

Posted: Wed May 19, 2004 3:22 am
by feyd

Code: Select all

$line = '<img src="somefile.gif" width="35" heigth="65">';

preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches); 
print_r($matches);
outputs

Code: Select all

Array
(
    &#1111;0] =&gt; ="somefile.gif"
    &#1111;1] =&gt; somefile.gif
    &#1111;2] =&gt; somefile
    &#1111;3] =&gt; gif
)

Posted: Wed May 19, 2004 3:48 am
by maqmus
When I try with the text line (as your example) it works.

But geting the data from the file it doesn't. I added echo (the source) and I get the image (I have a .gif file as $file[0])

Code: Select all

<?php
$data = "alterbanner/docs/lista.php";
$file = file($data);
array_shift($file);
preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$file[0],$matches); 
print_r($matches[1]);
echo "<br><br>".$file[0];
?>
This way I can see the image but not the filename.

Posted: Wed May 19, 2004 4:03 am
by feyd

Code: Select all

$file = array(
'<?php',
'<img src="somefile.gif" width="35" heigth="65">\n',
'<a href="somepage.html" target="_blank">My link</a>\n',
'<object.....(the whole code for flash files) ="myflashfile.swf" ...object>',
'?>'
);

foreach($file as $line)
{
	if(preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches))
	{
		echo $matches[1]."\n";
	}
}
outputs

Code: Select all

somefile.gif
somepage.html
myflashfile.swf

Posted: Wed May 19, 2004 4:23 am
by maqmus
I can't understand it.

When trying as you did, it works. But when I read from the file (lista.php) with the same content I get a blank page.
:?

Posted: Wed May 19, 2004 4:28 am
by feyd
dunno.. works perfectly fine as an file() call for me..

Posted: Wed May 19, 2004 5:26 am
by maqmus
I tried everything and nothing works.

When I use

Code: Select all

<?php
$file = file("alterbanner/docs/lista.php"); 
foreach($file as $line) 
{ 
if(preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches))
{ 
echo $matches[1]."<br>"; 
} 
} 
?>
I get a blank page (delayed). And when I echo the $line I get the gif image, flash file, etc.

Posted: Wed May 19, 2004 6:43 am
by John Cartwright

Code: Select all

<?php 
$file = array("alterbanner/docs/lista.php"); 
foreach($file as $line) 
{ 
if(preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches)) 
{ 
echo $matches[1]."<br>"; 
} 
} 
?>

Posted: Sun May 23, 2004 6:46 pm
by maqmus
It doesn't find the matches in the file.
I tried this:

Code: Select all

<?php
$file = array("alterbanner/docs/lista.php"); 
foreach($file as $line) 
{ 
if(preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches)) 
{ 
echo $matches[1]."<br>Hello_one"; 
} 
echo "Hello_two";
} 

?>
And I get only Hello_two on my page, like if there weren't any matches.