Jump to content

Check content of text file and if it finds www. it makes a link.


ztealmax

Recommended Posts

Hi is there anyway for a script to read a text file and if it finds some kind of url for example www.phpfreaks.com
it automaticly makes a clickable link of it?

my read text script:
[code]<?PHP require_once("theme.php");?>
<?php
// set file to read
$file = $_GET['text'];
// open file
$fh = fopen($file, 'r') /*or die('Could not open file!')*/;
// read file contents
$data = fread($fh, filesize($file)) /*or die('Could not read file!')*/;
// close file
fclose($fh);
// print file contents


?>
<DIV class="smallblacktext"><pre>
<? echo $data;?></DIV></pre>[/code]

Too many questions today? ;) hehe sorry about that, im full of question  ;D
Try  preg_replace_callback()
http://www.php.net/manual/en/function.preg-replace-callback.php
[code]<?php
function create_link($matches)
{
    //do some work here so a complete url is produced
    //with $matches, etc. 'http://...'
    $link = '<a href="' . $matches . '">' . $matches . '</a>';
    return $link;
}
echo preg_replace_callback(
          "**regular expression here**",
          "create_link", //function name!
          $data); //your variable
?> [/code]
if someone could help you with the actual regular expression to use there, that code should be good to go

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.