문제

I have searched and searched but it seems i'm the only one having this problem. So I want to convert line breaks from this $_GET variable. So this is my url:

test.php?name=Line%201\nLine%202\rLine%203\r\nLine%204

In my code I have tried:

print nl2br($_GET['name']); //doesn't work

I have also tried:

print str_replace(array("\r\n", "\r", "\n"), "<br>", $name); //doesn't work

Every time it prints out the original string (Line 1\nLine 2\rLine 3\r\nLine 4\n) with no change. However if I try it with a variable that is not passed in it always works. For instance:

$other = "Line 1\nLine 2\rLine 3\r\nLine 4\n";
print `nl2br($_GET['name']);
print str_replace(array("\r\n", "\r", "\n"), "<br>", $other);

I have also tried doing this in a tottaly new document, where I don't have any "sanitizing" scripts or any other scripts still can't get around it...

도움이 되었습니까?

해결책 2

I suspect this is because the characters sent are literally '\n' not a newline.

The GET request should probably be something like First+line%0ASecond+line;

If you can't change the request you can change the replace to escape the slashes:

print str_replace(array("\\r\\n", "\\r", "\\n"), "<br>", $name);

다른 팁

You have to decode this var first using urldecode(). It'll change url entities to actual chars.

Try replacing as normal strings str_replace(array("\\r\\n", "\\r", "\\n"), "<br>", $name); It'll replace literal eg \n, not new line.

Use urldecode() function which returns the decoded URL as a string.:

<?php

echo urldecode('Line%201\nLine%202\rLine%203\r\nLine%204'); 

?>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top