How to format this output?

Hi Folks,

Given the code below I get the file url.dat filled with urls. Now I would like to know how I can format the output so I get a list with one url per line?

Kind  regards,

Dweep
$regels= fopen('url.dat', 'w');
 
    for($i=1;$i<(count($urls))+1;$i++)  
             {
 
		 if (!stristr($urls[$i],$sGoogle))  
		         {
		         fputs($regels, $urls[$i])."<br>";
			
		         }
 
             }
  fclose($regels);

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.

Beverley PortlockCommented:
Try using '\n' instead of "<br>"
0
PeterdeBAuthor Commented:
Hi bportlock,

I tried that but it doesn't change the output.

Kind regards,

Dweep
0
dr_dedoCommented:
$regels= fopen('url.dat', 'w');
 
    for($i=1;$i<(count($urls))+1;$i++)  
             {
 
             if (!stristr($urls[$i],$sGoogle))  
                     {
                     fputs($regels, $urls[$i])."<br>\r\n";
                  
                     }
 
             }
  fclose($regels);
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Beverley PortlockCommented:
"I tried that but it doesn't change the output."

I've just noticed a little typo in your code

Try this

$regels= fopen('url.dat', 'w');
 
    for($i=1;$i<(count($urls))+1;$i++)  
             {
 
                 if (!stristr($urls[$i],$sGoogle))  
                         {
                         fputs($regels, $urls[$i]."\n");
                       
                         }
 
             }
  fclose($regels);
0
Rok-KraljCommented:
This code... What it writes to url.dat? Is the file empty?
0
PeterdeBAuthor Commented:
Hi dr dedo and bportlock,

I'm not sure what is going on but when using your code, it doesn't write a thing. I suppose I'm overlooking something somewhere but I'm not sure what?

I just transferred the script to my server to see what it does there but likewise, the file url.dat remains empty.....hmz.....

Kind regards,

Dweep
0
PeterdeBAuthor Commented:
Rok-Kralj,

Yes it is empty.

Dweep
0
PeterdeBAuthor Commented:
Ooops...I see something now. The only time data was written was when I used the 'a' instead of 'w' parameter.....I must have overlooked that when copying pasting my code to EE.

My apologize for that.....obviously 'a' appends text....but still no clue why 'w' doesnt write.

Dweep
0
Beverley PortlockCommented:
Maybe yor data is blank. Here is a "working version"

<?php
$urls = array("aaa", "bbb", "ccc");
$sGoogle = "a big string with bbb in it";

$regels= fopen('url.dat', 'w');
 
    for($i=1;$i<(count($urls))+1;$i++)  
             {
 
                 if (!stristr($urls[$i],$sGoogle))  
                         {
                         fputs($regels, $urls[$i]."\n");
                       
                         }
 
             }
  fclose($regels);
?>


which produces

bbb
ccc

0
PeterdeBAuthor Commented:
Hi Folks,

I'm stuck on the parameter ' a' the only which allows me to write anything to that file.

Kind regards,

Dweep
0
PeterdeBAuthor Commented:
bportlock,

No the data is not blank.

Kind regards,

Dweep
0
Beverley PortlockCommented:
can you supply some of the data and the expected output?
0
PeterdeBAuthor Commented:
Hi bportlock,

Yes I can. The data are urls. The expected output is a list with these urls. From a huge list of urls I pick out the ones not containing the google string.

Dweep

Ps is that sufficient?
0
Beverley PortlockCommented:
OK - I have simplifed the code slightly and it looks like this

<?php
$urls = array(
                    "http://www.google.com",
                    "http://www.yahoo.com",
                    "http://www.msn.com",
                    "http://www.google.co.uk"
             );
             
$sGoogle = "Google";




$regels= fopen('url.dat', 'w');

foreach ( $urls as $aUrl )
     if ( stripos( $aUrl, $sGoogle ) !== false )
          fputs( $regels, "$aUrl\n" );

fclose($regels);

?>


Which populates url.dat with this

http://www.google.com
http://www.google.co.uk



try that code. If it does not work for you then it is NOT a coding error, because it works here. It is likely to be an "environment" error such as incorrect permissions.
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
Beverley PortlockCommented:
Just noticing (after pressing SUBMIT) you want the ones that do not contain the Google string. Change

 if ( stripos( $aUrl, $sGoogle ) !== false )

to

 if ( stripos( $aUrl, $sGoogle ) === false )
0
PeterdeBAuthor Commented:
Thanks!
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.