• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

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

0
PeterdeB
Asked:
PeterdeB
1 Solution
 
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
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

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

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now