• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Auto Parse URLs with PHP

#1
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
 
#2
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.
 
S

Smifffy

Guest
#3
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
 
#4
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
 

SPeedY_B

I may actually be insane.
#5
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 :)
 
#8
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
 
#9
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);

?>
 

Members online

No members online now.

Latest posts

Latest profile posts

Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,014
Latest member
sanoravies