PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
maqmus
Forum Commoner
Posts: 30 Joined: Mon Mar 08, 2004 1:10 pm
Post
by maqmus » Wed May 19, 2004 2:09 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed May 19, 2004 2:38 am
try this:
Code: Select all
preg_match("#="?((/?[a-z0-9_-]+/?)+\.(html|gif|jpg|swf))"?#i",$line,$matches);
// $matches contains the .. matches
print_r($matches);
maqmus
Forum Commoner
Posts: 30 Joined: Mon Mar 08, 2004 1:10 pm
Post
by maqmus » Wed May 19, 2004 3:16 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed May 19, 2004 3:22 am
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
(
ї0] => ="somefile.gif"
ї1] => somefile.gif
ї2] => somefile
ї3] => gif
)
maqmus
Forum Commoner
Posts: 30 Joined: Mon Mar 08, 2004 1:10 pm
Post
by maqmus » Wed May 19, 2004 3:48 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed May 19, 2004 4:03 am
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
maqmus
Forum Commoner
Posts: 30 Joined: Mon Mar 08, 2004 1:10 pm
Post
by maqmus » Wed May 19, 2004 4:23 am
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed May 19, 2004 4:28 am
dunno.. works perfectly fine as an file() call for me..
maqmus
Forum Commoner
Posts: 30 Joined: Mon Mar 08, 2004 1:10 pm
Post
by maqmus » Wed May 19, 2004 5:26 am
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.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Wed May 19, 2004 6:43 am
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>";
}
}
?>
maqmus
Forum Commoner
Posts: 30 Joined: Mon Mar 08, 2004 1:10 pm
Post
by maqmus » Sun May 23, 2004 6:46 pm
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.