سؤال

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