Jump to content

[SOLVED] format url from a text


damonlee

Recommended Posts

Hello,

I have a script that reads from a text file, the text file contains http://www.someurl.com. which the end user will edit at their discrection.

I need the echo command to find the url and format it as a link.  I know I must be missing something simple, like calling the find_url function within the loop...

Here is my code:

<?php
function find_url($string){
//"www."
  $pattern_preg1 = '#(^|\s)(www|WWW)\.([^\s<>\.]+)\.([^\s\n<>]+)#sm';
  $replace_preg1 = '\\1<a href="http://\\2.\\3.\\4" target="_blank" class="link">\\2.\\3.\\4</a>';

//"http://"
  $pattern_preg2 = '#(^|[^\"=\]]{1})(http|HTTP|ftp)(s|S)?://([^\s<>\.]+)\.([^\s<>]+)#sm';
  $replace_preg2 = '\\1<a href="\\2\\3://\\4.\\5" target="_blank" class="link">\\2\\3://\\4.\\5</a>';
 
  $string = preg_replace($pattern_preg1, $replace_preg1, $string);
  $string = preg_replace($pattern_preg2, $replace_preg2, $string);

  return $string;
}
?>




<?

$filename = "uploadScript/files/recentEventsTXT.txt"; //here's the filename

$id = fopen($filename, "r"); //open the file
while ($data = fgetcsv($id, filesize($filename), "|")) //start a loop "|" sets pipe as delimiter
$table[] = $data; //put each line into its own entry in the $table array
fclose($id); //close file

echo "<table cellspacing='2'>\n";

foreach($table as $row)
{
echo "<tr>";
    foreach($row as $data)
    echo "<td width='400' height='18'>$data</td>";
echo "</tr>\n";
}

echo "</table>\n";

?>

I didn't write this code myself, they were examples found elsewhere, I'm trying to make them work together.  Thank you
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.