Auto Parse URLs with PHP

thebear

d(-.-)b
Joined
31 Jan 2003
Messages
364
I am opening a .txt file with the fopen function, and I want to know if there is a way of making all urls that are displayed links automatically
 
Yeah it's possible but not trivial. This forums uses such a function. I really can't say how it's done off the top of my head, but if you seach google you might find some scripts that do this or at least have this function as a part of it.
 
try opening the file - making it a varible then just use echo

<?php
$page = "";
$fp = fopen( "test.txt", "r" );
while( !feof( $fp ) )
$page .= fgets( $fp);
fclose( $fp );

echo "$page";
?>

:p

try that
 
That will just display the contents of the file. There are functions that will do this for you. The problem was parsing any url:s into acctual links:

Codewise:
http://whatever.com =>
<A HREF="http://whatever.com">http://whatever.com</A>

Displaywise:
http://whatever.com => http://whatever.com
 
Just wrote this...
PHP:
<?php

$p = fopen("test.txt", "r");
while(!feof($p))
{ $output = htmlspecialchars(fgets($p, 4096));
  $output = preg_replace("/((http(s?):\/\/)|(www\.))([\w\.]+)/i", "<a href=\"http$3://$4$5\" target=\"_blank\">$2$4$5</a>", $output);
  print $output;
}
fclose($p);

?>
It should work, Tried it locally on Windows, seems ok :)
 
Oooh, I really need to learn about reg-expressions some time. They are very handy.
 
well when i ran it on my server it parsed them into links - strange

o
i see - silly me you want www.blabla.com into <a href="http://www.blabla.com">www.blabla.com</a>

yea regex is the way to go then
 
well, I'm probably doing some newbish thing to make this not work... when I run the script I get line after line of:
Warning: Unknown modifier '/' in c:\apache\htdocs\nintel.php on line 54
 
Yeah, I get that to. It's this stupid forum code parser again. Try this instead:
PHP:
<?php

$p = fopen("test.txt", "r");
while(!feof($p))
{
  $output = htmlspecialchars(fgets($p, 4096));
  $output = preg_replace("/((http(s?):\\\/\\\/)|(www\\\.))([\\\w\\\.]+)/i", "<a href=\"http$3://$4$5\" target=\"_blank\">$2$4$5</a>", $output);
  print $output;
}
fclose($p);

?>
 
Originally posted by thebear
yeah, that works :cool:

the only problem is on URLs that have a / after the .com, it doesnt add it...(ex. http://site.com/page.php)

that has to do with the regexp. youd need to re-write it so that it will recognize everything beyon the .<tld>
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back