Reputation: 4024
I have an SQL file on Unix that I'm getting with PHP, cutting it from start and from the end and returning it into a textarea
with AJAX. The file is:
Set LINESIZE 1000
set timing on
set FEEDBACK off
SET PAGESIZE 50000
set COLSEP |
set trim on
set RECSEPCHAR ~
select * from tbvalid_value where discrete_code like '% %' and DISCRETE_CODE != 'U - Unreturned and billed'
;
exit;
after the exit;
i have another blank line.
What I want to return is only the select statement:
select * from tbvalid_value where discrete_code like '% %' and DISCRETE_CODE != 'U - Unreturned and billed'
To do this, firstly I'm chopping the first 8 rows with this function:
function str_chop_lines($str, $lines = 8) {
return implode("\n", array_slice(explode("\n", $str), $lines));
};
$sql = str_chop_lines($sqlString);
This cuts all the Set
Statements and the blank 8th line.
Then, I want to get rid from the last part of the string (from the ;
to the exit;
). For this I'm using this:
$finalSql = substr($sql, 0, strpos($sql, ';'));
But when I return the string to the client's side textarea
, I get 4 empty blank lines at the end of it, as seen in this picture (the red lines were added by me to illustrate the 4 blank lines):
I've tried to trim these lines with different methods:
$sqlReturnByAjax = substr($finalSql, 0, strrpos($finalSql, "\n"));
$sqlReturnByAjax = str_replace('/n/n', '/n', $finalSql);
$sqlReturnByAjax = rtrim($finalSql);
But non of these worked.
What am I missing ?
Upvotes: 0
Views: 122
Reputation: 15351
If the contents of the file on the server is
Set LINESIZE 1000
set timing on
set FEEDBACK off
SET PAGESIZE 50000
set COLSEP |
set trim on
set RECSEPCHAR ~
select * from tbvalid_value where discrete_code like '% %' and DISCRETE_CODE != 'U - Unreturned and billed'
;
exit;
and you only want the line that starts with the SELECT statement, you could use PHP's file()
to read file into an array of lines, then find the one you need. E.g.:
$sql = '';
$file = file('/path/to/file', FILE_IGNORE_NEWLINES);
foreach($file as $line) {
if(strpos($line, 'select') === 0) {
$sql = $line;
break;
}
}
Upvotes: 0