6 Replies - 1108 Views - Last Post: 02 March 2009 - 01:49 AM Rate Topic: -----

#1 Techno Guy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 27-May 08

Find and list all http links in txt file.

Posted 02 March 2009 - 12:57 AM

Hi,

I'v got a txt file that gets generated when ever I upload something, and it has the following info:

mlinks.txt file contents:
Uploading ofile1. down-links:
stat1=http://10.10.1.5/files/ofile.rar
stat2=Incomplete
stat3=Upload OK!

Uploading gfile1. down-links:
stat1=http://10.10.1.5/files/gfile.rar.html
stat2=Complete
stat3=Upload OK!

Uploading afile1. down-links:
stat1=http://10.10.1.5/files/afile.rar.html
stat2=Incomplete
stat3=Upload OK!


And then I use PHP to read the txt file and display it's contents but:
Now I have been messing around with preg_replace and similar commands to get all the other text out and just leave the the file links but no luck yet.
I was hoping that there is an easy way to just find all "http" * "html" links and list them. (* = anything)

Any thoughts... and/or help would be great, thanks! :)


So far I have managed to get the file open and display the contents:
<?
$myFile = "mlinks.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$theData = str_replace("\n", "<br />", "$theData");


echo $theData;


?>

This post has been edited by Techno Guy: 02 March 2009 - 01:00 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Find and list all http links in txt file.

#2 pr4y  Icon User is offline

  • Location: 127.0.0.1
  • member icon

Reputation: 35
  • View blog
  • Posts: 621
  • Joined: 19-September 08

Re: Find and list all http links in txt file.

Posted 02 March 2009 - 01:03 AM

If the data you are trying to replace has constant URL size, it would be easier to use substr() then to use preg_replace() (in my opinion... I hate regular expressions)


So yeah, if you are always going to have the same URL such as:

http://www.yoursite....th/to/file.html

Then I'd suggest just using:

substr()



I hope this helps!
Was This Post Helpful? 0
  • +
  • -

#3 Techno Guy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 27-May 08

Re: Find and list all http links in txt file.

Posted 02 March 2009 - 01:11 AM

Thanks for the fast reply, but the URL size will change in size in real life :(

Any other suggestions for that?
Was This Post Helpful? 0
  • +
  • -

#4 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Find and list all http links in txt file.

Posted 02 March 2009 - 01:15 AM

preg_match_all("/stat1=(\w\d)+\n/",$theData,$links);

$links = $links[1];

print_r($links);




That should fetch the links for you.

Also, instead of your str_replace() line, try $theData = nl2br($theData); instead :)

This post has been edited by Valek: 02 March 2009 - 01:21 AM

Was This Post Helpful? 0
  • +
  • -

#5 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Find and list all http links in txt file.

Posted 02 March 2009 - 01:26 AM

Actually, correction. I just tested it, and what I posted won't work. Try this instead:

<?php

$myFile = "mlinks.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
$theData = nl2br($theData);

echo $theData."<br /><br />";

preg_match_all("/stat1=(.+)\n/",$theData,$links);
$links = $links[1];
print_r($links);

?>


This one I have tested, and it does work.
Was This Post Helpful? 1
  • +
  • -

#6 Techno Guy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 27-May 08

Re: Find and list all http links in txt file.

Posted 02 March 2009 - 01:37 AM

Yea I tried the first one and it didn't work for me ether, but the 2nd post works great!

Thanks so much for your help!
Was This Post Helpful? 0
  • +
  • -

#7 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Find and list all http links in txt file.

Posted 02 March 2009 - 01:49 AM

My pleasure :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1