Skip to content

Instantly share code, notes, and snippets.

@recca0120
Created July 1, 2021 07:36
Show Gist options
  • Save recca0120/8a89a280cd637d7095db2c950817d0eb to your computer and use it in GitHub Desktop.
Save recca0120/8a89a280cd637d7095db2c950817d0eb to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
function getContents($params)
{
[$self, $file, $startLine, $startColumn, $endLine, $endColumn] = $params;
--$startLine;
--$startColumn;
--$endLine;
--$endColumn;
$handle = fopen($file, 'rb');
$i = -1;
$content = '';
while (($line = fgets($handle)) !== false) {
$i++;
if ($i < $startLine) {
continue;
}
if ($i === $startLine && $i === $endLine) {
$content .= substr($line, $startColumn, $endColumn - $startColumn);
break;
}
if ($i === $startLine) {
$content .= substr($line, $startColumn);
} else if ($i === $endLine) {
$content .= substr($line, 0, $endColumn);
break;
} else {
$content .= $line;
}
}
fclose($handle);
return $content;
}
echo var_export(json_decode(getContents($_SERVER['argv']), true), true).';';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment