lp__render_file_content( $file_path, $limit_args )
Reads the content of a file and returns it as a string with a line limit. You can specify whether to limit from the first or last lines and provide additional text to indicate truncated content.
Plugin
Namespace
Launchpad
Parameters
string
$file_path
The path of the file.
array
$limit_args
Array containing line, type, and text keys.
Return
string
The content of the file or an error message.
Source
File: \includes\lp-utility-functions.php
function lp__render_file_content( $file_path, $limit_args ) {
if ( file_exists( $file_path ) ) {
$content = file($file_path, FILE_IGNORE_NEW_LINES);
if (is_array($content)) {
$line_limit = lp__array_key_value( 'line', $limit_args );
$limit_type = lp__array_key_value( 'type', $limit_args );
$limit_text = lp__array_key_value( 'text', $limit_args );
if ($limit_type === 'last') {
$start_line = max(count($content) - $line_limit, 0);
} else {
$start_line = 0;
}
$limited_content = array_slice($content, $start_line, $line_limit);
if (count($content) > $line_limit) {
if( $limit_type === 'last' ){
$limited_content = array_merge([$limit_text],$limited_content);
} else {
$limited_content[] = $limit_text;
}
}
return implode("\n", $limited_content);
} else {
return "Error reading file content.";
}
} else {
return "File not found.";
}
}