Jump to content

array_slice - array_pop problem


GamerGun

Recommended Posts

Hello y'all.

 

I like to remove the last 2 lines from a flat file via PHP:

 

<?php

// Read the file into an array
$lines = file('/home/mqxfaokl/domains/informatiehooikoorts.nl/public_html/hooikoortsverwachting.txt');

// Pop the last item from the array
array_pop($lines);

// Join the array back into a string
$file = join('', $lines);

// Write the string back into the file 
$file_handle = fopen('/home/mqxfaokl/domains/informatiehooikoorts.nl/public_html/hooikoortsverwachting.txt', 'w');
fputs($file_handle, $file);
fclose($file_handle);

?>

 

This removes only the last line.

 

Now i thought this would work;

 

<?php

// Read the file into an array
$lines = file('/home/mqxfaokl/domains/informatiehooikoorts.nl/public_html/hooikoortsverwachting.txt');

// Pop the last item from the array
array_slice($lines, 0, -2);

// Join the array back into a string
$file = join('', $lines);

// Write the string back into the file 
$file_handle = fopen('/home/mqxfaokl/domains/informatiehooikoorts.nl/public_html/hooikoortsverwachting.txt', 'w');
fputs($file_handle, $file);
fclose($file_handle);

?>

 

But it doesn't do a thing? Any idea?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/204367-array_slice-array_pop-problem/
Share on other sites

array_pop is passed a reference to the array, array_splice retuns the array with the elements removed so you need to assign the output to a variable.

 

<?php

// Read the file into an array
$lines = file('/home/mqxfaokl/domains/informatiehooikoorts.nl/public_html/hooikoortsverwachting.txt');

// Pop the last item from the array
$lines = array_slice($lines, 0, -2);

// Join the array back into a string
$file = join('', $lines);

// Write the string back into the file 
$file_handle = fopen('/home/mqxfaokl/domains/informatiehooikoorts.nl/public_html/hooikoortsverwachting.txt', 'w');
fputs($file_handle, $file);
fclose($file_handle);

?>

$lines = file('http://informatiehooikoorts.nl/hooikoortsverwachting.txt');
echo '<pre>' . htmlentities(print_r($lines, true)) . '</pre>';

 

Outputs:

 

Array
(
    [0] => <h2>

    [1] => 		Gunstige hooikoortscondities

    [2] =>     </h2><p><strong>Zeist - 10 juni 2010 - Na de regen van gisteren vannacht is de lucht schoon. Er komen wel wat pollen in de lucht, maar nieuwe buien maken de lucht vandaag opnieuw pollenvrij.</strong></p><p><strong>Vandaag </strong>is er sprake van een vrij gunstige hooikoortssituatie. In de ochtend komen er geleidelijk wel wat pollen in de lucht, maar in de middag ontstaan er <a href="http://www.weeronline.nl/General/buienradar/129" class="autokeyword">buien</a>, die de lucht schoonspoelen. <br />

    [3] => <br />

    [4] => <strong>Morgen </strong>wordt ook een buiige dag met om die reden weinig pollen in de lucht.<br />

    [5] => <br />

    [6] => In het weekend is het aantal <a href="http://www.weeronline.nl/General/buienradar/129" class="autokeyword">buien</a> duidelijk lager en komen er meer pollen in de lucht.</p><p><strong>Meer informatie vindt u op de </strong><a href="http://www.weeronline.nl/Europa/Hooikoorts-Nederland/135"><strong>hooikoortspagina </strong></a><strong>van WeerOnline.</strong><br />

)

 

$lines = file('http://informatiehooikoorts.nl/hooikoortsverwachting.txt');
$lines = array_slice($lines, 0, -2);
echo '<pre>' . htmlentities(print_r($lines, true)) . '</pre>';

 

Outputs:

 

Array
(
    [0] => <h2>

    [1] => 		Gunstige hooikoortscondities

    [2] =>     </h2><p><strong>Zeist - 10 juni 2010 - Na de regen van gisteren vannacht is de lucht schoon. Er komen wel wat pollen in de lucht, maar nieuwe buien maken de lucht vandaag opnieuw pollenvrij.</strong></p><p><strong>Vandaag </strong>is er sprake van een vrij gunstige hooikoortssituatie. In de ochtend komen er geleidelijk wel wat pollen in de lucht, maar in de middag ontstaan er <a href="http://www.weeronline.nl/General/buienradar/129" class="autokeyword">buien</a>, die de lucht schoonspoelen. <br />

    [3] => <br />

    [4] => <strong>Morgen </strong>wordt ook een buiige dag met om die reden weinig pollen in de lucht.<br />

)

 

As expected... You must be missing something mate?

It's missing another row.

 

See; http://www.weeronline.nl/Go/GenericPages/WeatherSynopsis?synopsisCategory=WeatherForecastHayfeverExpectations

 

I get the information from there.

 

What i just don't want is (last 3 lines);

 

The blank line

The line with the image

The line with the link to their site ("Meer informatie vindt u op de hooikoortspagina van WeerOnline.")

 

But now with -2 its also deleting the line after "Morgen..."

 

I'm clueless...

 

(please note that this page changes every day, sometimes there is no blank line, sometimes there is, but the image and link to their site is always there).

Well, i guess i got it. The output of the script i use is;

 

<h2>
	Hooikoortssituatie gunstig 
    </h2><p><strong>Zeist - 11 juni 2010 - Het is vandaag bewolkt en buiig. Pollen die eventueel in de lucht komen worden er in het algemeen weer uitgespoeld. Na vandaag wordt de situatie een stuk minder gunstig.</strong></p><p><strong>Vandaag</strong> is het overwegend bewolkt en vanmiddag ontstaan er enkele regen- en onweersbuien. Er komen niet veel pollen in de lucht en de lucht wordt af en toe schoon gespoeld.<br />
<br />
<strong>Morgen</strong> zijn er nog veel wolken en kan er lokaal nog een buitje vallen. Al met al komen er wel meer pollen in de lucht dan vandaag.</p><p>Daarna is het overwegend droog en komt de zon er goed bij. Het aantal pollen in de lucht neemt toe.</p><p> </p><p><strong>Meer informatie vindt u op de </strong><a href="http://www.weeronline.nl/Europa/Hooikoorts-Nederland/135"><strong>hooikoortspagina </strong></a><strong>van WeerOnline.</strong><br />
<br />
<strong><br />
</strong></p><p><strong><img height="405" width="420" src="http://data.weeronline.nl/html/images/weernieuws/image/grassen.jpg" alt="" /><br />
</strong> </p><p> </p>

 

As you can see, the last 'blank lines' are because of the <p> constructions. So i have the str_replace these first, then i can go ahead with the rest...

 

Also, the line with the link to their website is on the same line as the one i want to keep.

Fixed it. In the first script, which scrapes the data, i had:

 

preg_match_all('=<p>(.*)</p>=siU', $result, $match, PREG_PATTERN_ORDER);
$match = $match[1];
foreach($match as $matches){
  $second = "<p>$matches</p>";
echo $second;
fwrite($fp, $second);
}

 

Which i changed to;

 

preg_match_all('=<p>(.*)</p>=siU', $result, $match, PREG_PATTERN_ORDER);
$match = $match[1];
foreach($match as $matches){
  $second = "<p>$matches</p>\n";
echo $second;
fwrite($fp, $second);
}

 

(see the \n at $second)

 

Now the text file looks like this;

 

<h2>
	Hooikoortssituatie gunstig 
    </h2><p><strong>Zeist - 11 juni 2010 - Het is vandaag bewolkt en buiig. Pollen die eventueel in de lucht komen worden er in het algemeen weer uitgespoeld. Na vandaag wordt de situatie een stuk minder gunstig.</strong></p>
<p><strong>Vandaag</strong> is het overwegend bewolkt en vanmiddag ontstaan er enkele regen- en onweersbuien. Er komen niet veel pollen in de lucht en de lucht wordt af en toe schoon gespoeld.<br />
<br />
<strong>Morgen</strong> zijn er nog veel wolken en kan er lokaal nog een buitje vallen. Al met al komen er wel meer pollen in de lucht dan vandaag.</p>
<p>Daarna is het overwegend droog en komt de zon er goed bij. Het aantal pollen in de lucht neemt toe.</p>
<p> </p>
<p><strong>Meer informatie vindt u op de </strong><a href="http://www.weeronline.nl/Europa/Hooikoorts-Nederland/135"><strong>hooikoortspagina </strong></a><strong>van WeerOnline.</strong><br />
<br />
<strong><br />
</strong></p>
<p><strong><img height="405" width="420" src="http://data.weeronline.nl/html/images/weernieuws/image/grassen.jpg" alt="" /><br />
</strong> </p>
<p> </p>

 

Which allows me to delete the last 8 rows, without losing other text.

 

Thanks!

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.