Skip to content

Instantly share code, notes, and snippets.

@wtnabe
Created March 20, 2010 03:26
Show Gist options
  • Save wtnabe/338460 to your computer and use it in GitHub Desktop.
Save wtnabe/338460 to your computer and use it in GitHub Desktop.
simple converter from file to array with PHP
<?php
function file2array( $file ) {
return file_exists( $file )
? array_filter( array_map( 'chop', file( $file ) ) )
: array();
}
qwdowjc
wcowkc
coiwecw
jow cj we
wowecu
weiojioew
<?php
/** Tester */
require_once( 'Pearified/Testing/SimpleTest/unit_tester.php' );
/** Reporter */
require_once( 'Pearified/Testing/SimpleTest/reporter.php' );
/** file2array */
require_once( dirname( __FILE__ ).'/file2array.php' );
class Test_File2array extends UnitTestCase {
function test_file2array_with_notexist_file() {
$a = file2array( dirname( __FILE__ ).'/notexist' );
$this->assertTrue( is_array( $a ) );
$this->assertEqual( 0, sizeof( $a ) );
}
function test_file2array_with_exist_file() {
$a = file2array( dirname( __FILE__ ).'/sample.txt' );
$this->assertTrue( is_array( $a ) );
foreach ( $a as $s ) {
$this->assertTrue( preg_match( '/\A\S.*\S\z/', $s ) );
}
}
}
if ( realpath( $_SERVER['SCRIPT_FILENAME'] ) == __FILE__ ) {
$test = new Test_File2array();
if ( SimpleReporter::inCli() ) {
$reporter = new TextReporter();
} else {
$reporter = new HtmlReporter();
}
$test->run( $reporter );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment