Separare un file di testo in righe con PHP

Separare un file di testo in righe con PHP

In PHP possiamo separare un file di testo in righe.

La soluzione è la seguente:


function split_text_file_into_lines($filepath) {
    $lines = [];

    if(!file_exists($filepath) || !is_readable($filepath)) {
        return $lines;
    }
    if(strstr(mime_content_type($filepath), 'text/') === false) {
        return $lines;
    }

    $contents = file_get_contents($filepath);
    $lines = preg_split('/\n|\r\n/', $contents);

    return $lines;
}

Torna su