remove empty elements from array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yaron
Forum Contributor
Posts: 157
Joined: Fri Aug 22, 2003 8:40 am

remove empty elements from array

Post by yaron »

Hello all,
I want to process an html file.
I want to transfer it to a regular text file so I first have to remove all tags and then to remove all blank lines in the file.

$file=implode('|', file ($path));
$fileArr = strip_tags($file);

well, this remove all tags and creats a string out of the file with | as the delimiter.
before I turn it into a string I want to remove all blank line on the file.
do I need to use array_filter?
if so how?
Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

e.g.

Code: Select all

<?php
// an example array
$file = array("a\n\r", "\n\r", "\n\r", "b\n\r");

// remove linebreaks
$file = array_map('trim', $file);

// empty strings evaluate to FALSE, so there's no need for any test-function
$file = array_filter($file);

print_r($file);
?>
Post Reply