How to transform this code a bit?

Hi Folks,

Given the piece of code I attached I would like to change a few things. Currently it takes the input from a textarea filled with urls. Now I would like it to take the input from a file which contains the urls (one per line). Now I manage to get the links from the file but I fail to feed the urls from the textfile to the for loop at the end.....If you need more info please ask!

Thanks in advance,

Kind regards,

Dweeep




/*..................................................................*/
/* Original piece of code unmodified yet */
/*..................................................................*/
 
<center><form action="" method="post"><textarea name="urls" cols="70" rows="9"></textarea>
 
if (isset($_POST['urls'])) 
{
    $arrTempurls=$_POST['urls']; /* Temporary array of urls */
    $parts = explode("\n", $arrTempurls); // Finding all urls
/*..................................................................*/
        foreach ($parts as $sUrl)  // for loop 
        {
          DoSomething($sUrl);
        }
/*..................................................................*/
 
/*..................................................................*/
/* Use file with urls for input instead of textarea, something like this  
/*..................................................................*/
 
 
$file = fopen("links.dat", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
  {
  $sUrl = fgets($file)."<br />";
  }
fclose($file);

Open in new window

PeterdeBAsked:
Who is Participating?
 
shiraztiConnect With a Mentor Commented:
Try this one, very minor change....

$sUrl .= fgets($file)."<br />";
<?php
 
$file = fopen("links.dat", "r") or exit("Unable to open file!");
//Output a line of the file until the end is reached
while(!feof($file))
  {
  $sUrl .= fgets($file)."<br />";
  }
fclose($file);
 
	//echo $sUrl;
 
?>

Open in new window

0
 
PeterdeBAuthor Commented:
Hi Shirazti!

This is what I figured out myself just a minute ago. But you're first to post so points are yours! ;-)
Btw what does the period do in > $sUrl .= fgets ?

kind regards,

Dweep

 
while(!feof($file))
  {
  $sUrl = fgets($file);
  
  }
fclose($file);

Open in new window

0
 
PeterdeBAuthor Commented:
Thank you!
0
 
shiraztiCommented:
Hi Dweep,

Sorry I forgot to explain, I thought you may understand

First check the Example code below....

.= is a combined assignment operator

We use fgets() to read a file line by line. I guess you got the point anyway if its not clear please leave a comment.

Thank you!

FYI
file_get_contents() can also be used to read the contents of a file. I don't know which one is better ;-)

<?php

//PHP 4.3 or higher
$load = file_get_contents('links.dat');
$split = explode("\n", $load);

foreach($split as $key) {
      echo $key.'<br />';
}

?>
<?php
 
$fruits = 'Apple';
 
echo $fruits;
echo "<br />";
 
//Method 1
$fruits = $fruits.", Orange";
 
echo $fruits;
echo "<br />";
 
// Which is same as Method 1
 
$fruits .= ", Banana";
 
echo $fruits;
echo "<br />";
 
?>

Open in new window

0
All Courses

From novice to tech pro — start learning today.