简体   繁体   中英

Why am I getting blank lines

I'm iterating through a $_POST to write each item into a text file. It's working, but despite having used 'strlen' to avoid blank lines, I'm still getting some. Can anyone tell me why? Here's my code:

$entries = "";
$filename = "test.txt";
foreach($_POST as $value) {
    if( is_array( $value ) ) {
        foreach( $value as $subvalue ) {
            if (strlen($subvalue) > 5) {
                $entries .= $subvalue . PHP_EOL;
            }
        }
    }
}
file_put_contents($filename, $entries);

Try below code for your solution:

$entries = "";
$filename = "test.txt";
foreach($_POST as $value) {
    if( is_array( $value ) ) {
        foreach( $value as $subvalue ) {
            if (strlen(trim($subvalue)) > 5) {
                $entries .= $subvalue . PHP_EOL;
            }
        }
    }
}
file_put_contents($filename, $entries);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM