Home > database >  How can I remove the \n at the end of a line but keep the line breaks in a file? In FastCGI request
How can I remove the \n at the end of a line but keep the line breaks in a file? In FastCGI request

Time:10-19

Currently the code that I have is in this question (I don't know if it is necessary to add the code, although it is the same that I have in that question, but if you think I should add the code here tell me and I will do it)

Running my program with the code above I get this output:

Ok("X-Powered-By: PHP/8.1.11\r\nContent-type: text/html; charset=UTF-8\r\n\r\nFirst file")

But the content of my PHP file is this:

<?php

echo "First file";

?>

Clearly in the output of the program that I show, the output of my PHP program is there, but it returns it to me like this

\r\n\r\nFirst file

So, the first thing I thought was to try to treat the output of my PHP program using regular expressions, for example, after "\r\n\r\n" it would return what I think would be the "output" of my file, and works. But there is a bug with that, if I have longer PHP files, like this for example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
</html>
<?php

echo "PHP file";

header("Location: form.php");

?>

The output I get when executing the FastCGI request is this:

Ok("Status: 302 Found\r\nX-Powered-By: PHP/8.1.11\r\nLocation: form.php\r\nContent-type: text/html; charset=UTF-8\r\n\r\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <title>Document</title>\n</head>\n<body>\n    \n</body>\n</html>\nPHP file")

As you can see, after each newline I get a \n, but for example, if I want to show that in a web browser, that would not be a good idea, or at least the browser would not give the output I expect since there is lots of \n, and for obvious reasons I can't deal with them using regular expressions. So my question is, how can I remove the \n but still have the line breaks in my html code? Since an html file without line breaks would be unmaintainable.

CodePudding user response:

You haven't posted your Rust code, but it seems like you're confused by the debug output. The literal character sequences \n (or \r, for that matter) are not in the response string, they are literal newlines and carriage returns (ASCII codes 10 and 13). When printing with Debug, Rust tries to make it easier for you to spot them by showing them as \n, just like they would appear in a string literal.

Consider this example:

fn main() {
    let text = "Hello\nWorld";
    println!("With Debug:");
    println!("{:?}", text);
    println!("With Display:");
    println!("{}", text);
}

(playground)

The output is:

With Debug:
"Hello\nWorld"
With Display:
Hello
World

On another note, \r\n\r\n separates HTTP headers from the response body (or more accurately, every header is followed by \r\n, and the end of headers is marked by another \r\n). If you want to parse separate parts of the HTTP response (status code, headers, body), I would suggest using a proper HTTP parser instead of regular expressions. Or, just return the response verbatim to the user - it seems like a well formed HTTP response.

CodePudding user response:

easily when trying to show it on html just use nl2br() , you can take a look on it here https://www.php.net/manual/en/function.nl2br.php

  • Related