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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

shiraztiCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.