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
https://forums.phpfreaks.com/topic/32143-solved-format-url-from-a-text/
Share on other sites

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.