Created
March 20, 2010 03:26
-
-
Save wtnabe/338460 to your computer and use it in GitHub Desktop.
simple converter from file to array with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function file2array( $file ) { | |
return file_exists( $file ) | |
? array_filter( array_map( 'chop', file( $file ) ) ) | |
: array(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
qwdowjc | |
wcowkc | |
coiwecw | |
jow cj we | |
wowecu | |
weiojioew | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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